4. Find in a two-dimensional array

In a two-dimensional array, each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function, input such a two-dimensional array and an integer, and determine whether the array contains the integer.

class solution:
    def find(self,target,array):
        if not array:
            return None
        m,n = len(array),len(array[0])#这样是可以的 显示行和列
        row,col = 0,n-1
        while row <=m-1 and col >= 0:
            if array[row][col] == target:
                return True
            elif array[row][col] < target:
                row += 1
            elif array[row][col] > target:
                col -= 1
        return False```

这里这种解法比直接遍历更好。(用for不灵活,而且容易陷入思维误区)
这种显示行列蛮好
Published 16 original articles · Likes0 · Visits 162

Guess you like

Origin blog.csdn.net/qq_43275748/article/details/102641265