Seawolf 发表于 2020-9-11 09:14:02

Leetcode 74. Search a 2D Matrix

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 from left to right.
The first integer of each row is greater than the last integer of the previous row.
Example 1:

Input:
matrix = [
,
,

]
target = 3
Output: true
Example 2:

Input:
matrix = [
,
,

]
target = 13
Output: false

class Solution:
    def searchMatrix(self, matrix: List], target: int) -> bool:
      if matrix == None or len(matrix) == 0 or len(matrix) == 0:
            return False
      
      row, col = 0, len(matrix) - 1
      while row < len(matrix) and col >= 0:
            curt = matrix
            
            if curt < target:
                row +=1
            elif curt > target:
                col -= 1
            else:
                return True
      return False
页: [1]
查看完整版本: Leetcode 74. Search a 2D Matrix