-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInRotatedSortedArray.py
More file actions
38 lines (33 loc) · 934 Bytes
/
SearchInRotatedSortedArray.py
File metadata and controls
38 lines (33 loc) · 934 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
class Solution:
# @param A, a list of integers
# @param target, an integer to be searched
# @return an integer
def search(self, A, target):
pos = 0
for index in xrange(len(A) - 1):
if A[index] > A[index + 1]:
pos = index
break
firstStart = 0
firstEnd = pos
secondStart = pos + 1
secondEnd = len(A) - 1
start = 0
end = len(A)
if A[firstStart] <= target <= A[firstEnd]:
start = firstStart
end = firstEnd
else:
start = secondStart
end = secondEnd
while start <= end:
mid = (start + end) / 2
if A[mid] == target:
return mid
elif A[mid] > target:
end = mid - 1
else:
start = mid + 1
return -1
sol = Solution()
print sol.search([1], 1)