-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
39 lines (36 loc) · 1.02 KB
/
solution.py
File metadata and controls
39 lines (36 loc) · 1.02 KB
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
35
36
37
38
39
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
m = len(matrix)
n = 0 if m == 0 else len(matrix[0])
if m == 0 or n == 0:
return False
low = 0
high = m - 1
row_index = -1
while low <= high:
mid = (low + high) / 2
if matrix[mid][n - 1] < target:
low = mid + 1
elif matrix[mid][0] > target:
high = mid - 1
else:
row_index = mid
break
if row_index == -1:
return False
low = 0
high = n - 1
while low <= high:
mid = (low + high) / 2
if matrix[row_index][mid] < target:
low = mid + 1
elif matrix[row_index][mid] > target:
high = mid - 1
else:
return True
return False