74. Search a 2D Matrix
class Solution(object):
def searchMatrix(self, matrix, target):
top, bot = 0, len(matrix) - 1
while top <= bot:
mid = (top + bot) // 2
if target > matrix[mid][-1]:
top = mid + 1
elif target < matrix[mid][0]:
bot = mid - 1
else:
break
if not (top <= bot):
return False
mid = matrix[(top + bot) // 2]
l, r = 0, len(matrix[0]) - 1
while l <= r:
m = (l + r) // 2
if mid[m] > target:
r = m - 1
elif mid[m] < target:
l = m + 1
else:
return True
return False