-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch_2.py
62 lines (32 loc) · 1.31 KB
/
search_2.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
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
def search(list_rotated,target):
'''
Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
这两个题目最重要的地方是:首先要判断哪一段是单调的
上一题,不允许重复,则如果list[m]>=list[n],则[n,m]为递增
这一题,允许重复,则list[m]>list[n],则[n,m]为递增,等于的时候就不一定,需要n+1看一下
'''
first = 0
last = len(list_rotated)-1
while(first != last):
mid = (first + last)//2
if list_rotated[mid] == target:
return mid
if list_rotated[first] < list_rotated[mid]:
if (list_rotated[first]<=target and target<list_rotated[mid]):
last = mid
else:
first = mid+1
if list_rotated[first] > list_rotated[mid]:
if (list_rotated[mid]<target and target<=list_rotated[last]):
first = mid+1
else:
last = mid
else:
first +=1
return -1
if __name__ =='__main__':
list1=[1,3,1,1,1]
t=1
print(search(list1,t))