二维数组中的查找(python实现)

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

class Solution:
    # array 二维列表
    def Find(self, target, array):
        # write code here
        m, n = len(array),len(array[0])
        i, j = 0, n-1
        while i >= 0 and i <= m-1 and 0 <= j and j <= n-1:
            cur = array[i][j]
            if cur == target:
                return True
            elif cur > target:
                j = j -1
            else:
                i = i + 1
        return False

猜你喜欢

转载自blog.csdn.net/m0_37422289/article/details/79447171