Leetcode 240. Search a 2D Matrix II
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.
Example:
Consider the following matrix:
[
,
,
,
,
]
Given target = 5, return true.
Given target = 20, return false.
class Solution:
def searchMatrix(self, matrix, target):
"""
:type matrix: List]
:type target: int
:rtype: 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]