-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path74.py
35 lines (34 loc) · 983 Bytes
/
74.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
35
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix or not matrix[0]:
return False
rows = len(matrix)
while rows > 0:
if matrix[rows - 1][0] < target:
return self.bin_search(matrix[rows - 1], target)
elif matrix[rows - 1][0] == target:
return True
else:
rows -= 1
return False
def bin_search(self, data, target):
"""
:type data: List[int]
:type target: int
:rtype: bool
"""
low, high = 0, len(data)
while low < high:
mid = low + (high - low) / 2
if data[mid] == target:
return True
elif data[mid] < target:
low = mid + 1
else:
high = mid
return False