-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch_1.py
More file actions
52 lines (33 loc) · 1.16 KB
/
Copy pathsearch_1.py
File metadata and controls
52 lines (33 loc) · 1.16 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
def search(list_rotated,target):
'''
Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
有序数列查找,首先想到二分
'''
len_list = len(list_rotated)
first = 0
last = len_list-1
if first==last:
return first
while first != last:
mid = (first+last)//2
if list_rotated[mid] == target:
return mid
if list_rotated[mid]>= list_rotated[first]:
if (list_rotated[first]<target and target<list_rotated[mid]):
last = mid
else:
first = mid+1
else:
if (list_rotated[mid]<target and target<=list_rotated[last]):
first = mid+1
else:
last = mid
return -1
if __name__ =='__main__':
list1=[4]
t=4
print(search(list1,t))