解题思路:
利用好排序信息,从右上角开始检查,做O(n+m)次判断即可找到是否含有对应的target。
具体代码如下:
class Solution:
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if len(matrix) == 0:
return False
height = len(matrix)
length = len(matrix[0])
first = 0 #[0, height - 1]
second = length - 1 #[0, length - 1]
while first < height and second >= 0:
if matrix[first][second] > target:
second -= 1
elif matrix[first][second] < target:
first += 1
elif matrix[first][second] == target:
return True
return False