000074-Search-a-2D-Matrix
Problem
Solution
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if not matrix:
return False
rows, cols = len(matrix), len(matrix[0])
left, right = 0, rows * cols - 1
while left <= right:
mid = (left + right) // 2
m_r, m_c = divmod(mid, cols)
if matrix[m_r][m_c] == target:
return True
elif matrix[m_r][m_c] < target:
left = mid + 1
else:
right = mid - 1
return FalseLast updated