forked from huxiaoman7/leetcodebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33_SearchinRotatedSortedArray.py
More file actions
45 lines (34 loc) · 1016 Bytes
/
Copy path33_SearchinRotatedSortedArray.py
File metadata and controls
45 lines (34 loc) · 1016 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
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/2/28 4:53 PM
# @Author : huxiaoman
# @File : 33_SearchinRotatedSortedArray.py
# @Package : LeetCode
# @E-mail : charlotte77_hu@sina.com
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
start = 0
end = len(nums)-1
while start <= end:
mid = (start+end)/2
if nums[mid] == target:
return mid
if nums[mid] >= nums[start]:
if target >= nums[start] and target<nums[mid]:
end = mid-1
else:
start = mid+1
if nums[mid] <nums[end]:
if target>nums[mid] and target<=nums[end]:
start = mid+1
else:
end=mid-1
return -1
if __name__=='__main__':
s = Solution()
print s.search([4,5,6,7,0,1,2],0)