leetcode-240搜索二维矩阵II

搜索二维矩阵II

class Solution:
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        for i in matrix:
            if target in i:
                return True
        return False

还有两种思路:

  一、从右上角开始搜索,如果I(x, y) < target,则y--;如果I(x, y) > target;则x++。以5为例:

    

  二、获取一行,先判断target是否在当前行决定是否搜索。target > line[0] and target < line[-1]

posted @ 2019-05-17 10:00  天涯&海角  阅读(163)  评论(0编辑  收藏  举报