-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode0033Modified.py
50 lines (45 loc) · 1.36 KB
/
Leetcode0033Modified.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
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if not nums:
return -1
if len(nums) == 1:
if nums[0] == target:
return 0
return -1
start = 0
end = len(nums)-1
while(start+1<end):
middle = (start+end)/2
# print start, end, middle
if nums[middle] == target:
return middle
if nums[start]<nums[middle]<target:
start = middle
continue
if nums[start]<=target<nums[middle]:
end = middle
continue
if nums[middle]<nums[start]<=target:
end = middle
continue
if target<=nums[start]<nums[middle]:
start = middle
continue
if nums[middle]<target<=nums[end]:
start = middle
continue
if target<nums[middle]<=nums[end]:
end = middle
continue
return -1
print start, end
if nums[start] == target:
return start
if nums[end] == target:
return end
return -1