leetcode中74题搜索二维矩阵用python3实现,leetcode中没有python3的实现,自己实现的,只用了一次二分,前一次没用二分,代码如下:
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
l, r, ant = 0, len(matrix[0]) - 1, -1
for i in range(len(matrix)):
if matrix[i][-1] > target:
ant = i
break
elif matrix[i][-1] == target:
return True
if i == len(matrix) - 1 and ant == -1:
return False
while l <= r:
mid = (l + r)//2
if matrix[ant][mid] == target:
return True
elif matrix[ant][mid] > target:
r = mid - 1
else:
l = mid + 1
return False