题目描述 用了两个二分查找法。当然也可以把matrix转为数组来索引
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
low = 0
high = len(matrix)-1
mid = 0
while low<=high:
mid = (high+low)//2
if target < matrix[mid][0]:
high = mid-1
elif target>matrix[mid][-1]:
low = mid+1
else:
break
i = mid
low = 0
high = len(matrix[0])-1
while low<=high:
mid = (low+high)//2
if target==matrix[i][mid]:
return True
if target<matrix[i][mid]:
high = mid-1
else:
low = mid+1
return False
运行结果: