美文网首页
240. Search a 2D Matrix II

240. Search a 2D Matrix II

作者: Mree111 | 来源:发表于2019-10-21 12:10 被阅读0次

    Description

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

    Integers in each row are sorted in ascending from left to right.
    Integers in each column are sorted in ascending from top to bottom.

    Solution

    class Solution:
        def searchMatrix(self, matrix, target):
            """
            :type matrix: List[List[int]]
            :type target: int
            :rtype: bool
            """
            i = len(matrix) -1
            j = 0
            while i>=0 and j < len(matrix[0]):
                if matrix[i][j]==target:
                    return True
                elif matrix[i][j]> target:
                    i-=1
                else:
                    j+=1
            return False
    

    相关文章

      网友评论

          本文标题:240. Search a 2D Matrix II

          本文链接:https://www.haomeiwen.com/subject/sqctvctx.html