forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_in_rotated_sorted_array.py
More file actions
31 lines (30 loc) · 1.01 KB
/
search_in_rotated_sorted_array.py
File metadata and controls
31 lines (30 loc) · 1.01 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
# -*- coding: utf-8 -*-
class Solution:
"""
@param A : a list of integers
@param target : an integer to be searched
@return : an integer
"""
def search(self, A, target):
# write your code here
start, end = 0, len(A) - 1
while start <= end:
mid = (start + end) / 2
if A[mid] == target:
return mid
'''
注意,因为数组是旋转排序,所以先不要与target比较。
1. 如股票A[mid] >= A[start],那么A[mid]肯定在前半个上升序列。
2. 反之A[mid]则在后半个上升序列。
'''
elif A[start] <= A[mid]:
if (A[start] <= target) and (A[mid] > target):
end = mid - 1
else:
start = mid + 1
else:
if (A[end] >= target) and (A[mid] < target):
start = mid + 1
else:
end = mid - 1
return -1