1. 程式人生 > >【牛客網】陣列題目I---【Python】

【牛客網】陣列題目I---【Python】

class Solution:
    # array 二維列表
    def Find1(self, target, array):
        # write code here
        for i in range(len(array)):
            for j in range(len(array[0])):
                if array[i][j] == target:
                    return True
                elif array[i][j]>target:
                    break
        return False

    """
    從左下角找
    如果target大於這個數,則肯定在右邊
    如果target小於這個數,則一定在上方
    """
    def Find2(self,target,array):
        row = len(array)
        col = len(array[0])
        i = row -1
        j = 0
        while i>=0 and j<col:
            if array[i][j] == target:
                return True
            elif array[i][j]>target:
                i-=1
            elif array[i][j]<target:
                j+=1
        return False

    """
       從右上角找
       如果target大於這個數,則肯定在下方
       如果target小於這個數,則一定在左邊
       """

    def Find3(self, target, array):
        row = len(array)
        col = len(array[0])
        i = 0
        j = col-1
        while i<row and j>=0:
            if array[i][j] == target:
                return True
            elif array[i][j] > target:
                j -= 1
            elif array[i][j] < target:
                i += 1
        return False


a=Solution()
array=[[1,6,8],
       [2,9,10]]
print(a.Find2(8,array))