-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
34 lines (30 loc) · 1012 Bytes
/
solution.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
from typing import List
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
rows, cols = len(matrix), len(matrix[0])
top, bot = 0, rows-1
while top <= bot:
row = (top + bot) // 2
if target > matrix[row][-1]:
top = row + 1
elif target < matrix[row][0]:
bot = row - 1
else:
break
if not (top <= bot):
return False
row = (top + bot) // 2
left, right = 0, cols-1
while left <= right:
middle = (left + right) // 2
if target > matrix[row][middle]:
left = middle + 1
elif target < matrix[row][middle]:
right = middle - 1
else:
return True
return False
if __name__ == '__main__':
matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]]
target = 3
print(Solution().searchMatrix(matrix, target))