-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path0074.py
33 lines (29 loc) · 824 Bytes
/
0074.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
class Solution:
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix:
return False
r, c = len(matrix), len(matrix[0])
left, right = 0, r*c
while left < right:
mid = (left+right)//2
m, n = mid//c, mid%c
if matrix[m][n] == target:
return True
elif matrix[m][n] < target:
left = mid + 1
else:
right = mid
return False
if __name__ == "__main__":
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
print(Solution().searchMatrix(matrix, target))