-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsearch_a_2d_matrix.py
34 lines (29 loc) · 1.19 KB
/
search_a_2d_matrix.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
29
30
31
32
33
34
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
'''
1. find target row
2. find target element from 1
'''
rowLen, colLen = len(matrix), len(matrix[0])
top, bottom = 0, rowLen - 1
while top <= bottom:
centreRow = top + (bottom - top) // 2
if target > matrix[centreRow][-1]: # target below centreRow
top = centreRow + 1
elif target < matrix[centreRow][0]: # target above centreRow
bottom = centreRow - 1
else: # target in centreRow
break
if not (top <= bottom): # element not found in matrix
return False
# centreRow = top + (bottom - top) // 2
left, right = 0, colLen - 1
while left <= right:
middle = left + (right - left) // 2
if target > matrix[centreRow][middle]: # target towards right
left = middle + 1
elif target < matrix[centreRow][middle]: # target towards left
right = middle - 1
else:
return True
return False