-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.py
More file actions
41 lines (35 loc) · 1.3 KB
/
BinarySearch.py
File metadata and controls
41 lines (35 loc) · 1.3 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
40
41
class Solution(object):
def binary_search(self, matrix,target,start,vertical):
low = start
high = len(matrix[0])-1 if vertical else len(matrix)-1
while high >= low:
mid = (low + high) // 2 #rounded number as return
if vertical:
if matrix[start][mid] < target:
low = mid + 1
elif matrix[start][mid] > target:
high = mid - 1
else:
return True
else:
if matrix[mid][start] < target:
low = mid + 1
elif matrix[mid][start] > target:
high = mid -1
else:
return True
return False
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix:
return False
for i in range (min(len(matrix), len(matrix[0]))):
vertical_found = self.binary_search(matrix, target, i, True)
horizontal_found = self.binary_search(matrix, target, i, False)
if vertical_found or horizontal_found:
return True
return False