Ideas
算法:二分查找
Code
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
m = len(matrix)
n = len(matrix[0])
for row in matrix:
if row[0] > target or row[n - 1] < target: continue
l = 0
r = n - 1
while(l <= r):
mid = (l + r) // 2
if row[mid] == target: return True
elif row[mid] > target: r = mid - 1
else: l = mid + 1
return False
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
for i in matrix:
for j in i:
if j == target:
return True
return False