forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-search.py
More file actions
executable file
·70 lines (57 loc) · 2.05 KB
/
binary-search.py
File metadata and controls
executable file
·70 lines (57 loc) · 2.05 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
First, see if target is larger then the largest or smaller then the smallest
If it is out of range, it is not in the `nums`.
Second, we use two pointers `l`, `r` to navigate our *binary search*.
For every iteration, we choose the index, middle between `l` and `r` as `p`.
If the value at `p` is larger than the target, we search the left-half by `r = p-1`.
If the value at `p` is smaller than the target, we search the right-half by `l = p+1`.
Continue this proccess, until we find the value.
If we can't find the value, then `l` and `r` are going to collapse. `return -1`
Another solution is to use [bisect](https://docs.python.org/2.7/library/bisect.html).
"""
class Solution(object):
def search(self, nums, target):
if nums is None or len(nums)==0: return -1
if target<nums[0] or nums[-1]<target: return -1 #test is out of range
l = 0
r = len(nums)-1
while r>=l:
if nums[l]==target: return l
if nums[r]==target: return r
p = (l+r)/2
if nums[p]==target:
return p
elif nums[p]>target:
r = p-1
else:
l = p+1
return -1
import bisect
class Solution(object):
def search(self, nums, target):
if nums is None or len(nums)==0: return -1
if target<nums[0] or nums[-1]<target: return -1 #check if out of range
i = bisect.bisect_left(nums, target)
#check if target in `nums`
if nums[i]==target: return i
else: return -1
# 2020/7/19
class Solution(object):
def search(self, nums, target):
if not nums: return -1
l = 0
r = len(nums)-1
while True:
if l>r: break
if target<nums[l]: return -1
if nums[r]<target: return -1
if target==nums[l]: return l
if target==nums[r]: return r
m = (l+r)/2
if target==nums[m]:
return m
elif target<nums[m]:
r = m-1
else:
l = m+1
return -1