-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
searchMatrix.py
28 lines (26 loc) · 1.09 KB
/
searchMatrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Method 1: Binary Search on each row, if the first element of the row is already bigger than target, then skip
# Time Complexity: O(mlogn)
def searchMatrix1(self, matrix, target):
def helper(low, high, row):
while low <= high:
mid = (low + high) // 2
if matrix[row][mid] < target: low = mid + 1
elif matrix[row][mid] > target: high = mid - 1
else: return True
return False
if not matrix or not matrix[0]: return False
for i in range(len(matrix)):
if matrix[i][0] > target: return False
elif matrix[i][0] == target: return True
if helper(0, len(matrix[i]) - 1, i): return True
return False
# Method 2: compare the element with top-right corner, and reduce the search range
# Time complexity: O(m + n)
def searchMatrix2(self, matrix, target):
if not matrix or not matrix[0]: return False
row, col = 0, len(matrix[0]) - 1
while row < len(matrix) and col >= 0:
if matrix[row][col] > target: col -= 1
elif matrix[row][col] < target: row += 1
else: return True
return False