forked from HarshRangwala/Interview-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Search.py
More file actions
50 lines (44 loc) · 909 Bytes
/
Binary Search.py
File metadata and controls
50 lines (44 loc) · 909 Bytes
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
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 8 11:48:05 2020
@author: Anuj
"""
num= [-1,0,3,5,9,12]
#num = sorted(num)
target = 90
'''
low = 0
high = len(num) - 1
#mid = (low+high)//2
print(low)
print(high)
while low<=high:
mid = (low+high)//2
if num[mid]<target:
low = mid + 1
elif num[mid]>target:
high = mid - 1
else:
print(mid)
break
print(-1)
'''
class Solution(object):
def search(self, num, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
low = 0
high = len(num) - 1
while low<=high:
mid = (low+high)//2
if num[mid]<target:
low = mid + 1
elif num[mid]>target:
high = mid -1
else:
return mid
return -1
print(Solution.search(0, num, target))