-
Notifications
You must be signed in to change notification settings - Fork 1
/
33. Search in Rotated Sorted Array_v3.py
104 lines (88 loc) · 3.59 KB
/
33. Search in Rotated Sorted Array_v3.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Solution:
#def search(self, nums: List[int], target: int) -> int:
def search(self, nums: list, target: int) -> int:
left_index = 0
right_index = len(nums) -1
while left_index < right_index:
mid_index = (left_index + right_index) // 2
left_val = nums[left_index]
right_val = nums[right_index]
mid_val = nums[mid_index]
if mid_val == target:
return mid_index
if mid_val <= left_val: # pivot is on left part:
if mid_val < target and target <= right_val: # target is in range
left_index = mid_index + 1
else: # target <= right_val
right_index = mid_index -1
if left_val <= mid_val: # the pivot is not in this range:
if left_val <= target and target < mid_val:
right_index = mid_index -1
else:
left_index = mid_index + 1
if nums[left_index] == target:
return left_index
return -1
sol = Solution()
nums = [3,1]
target = 0
i = sol.search(nums,target)
print(i)
"""
Some change of sides:
"""
class Solution:
def search(self, nums: List[int], target: int) -> int:
#def search(self, nums: list, target: int) -> int:
left_index = 0
right_index = len(nums) -1
while left_index < right_index:
mid_index = (left_index + right_index) // 2
left_val = nums[left_index]
right_val = nums[right_index]
mid_val = nums[mid_index]
if mid_val == target:
return mid_index
if mid_val <= left_val: # pivot is on left part:
if target > mid_val and target <= right_val: # target is in range
left_index = mid_index + 1
else: # target <= right_val
right_index = mid_index -1
if left_val <= mid_val: # the pivot is not in this range:
if target >= left_val and target < mid_val:
right_index = mid_index -1
else:
left_index = mid_index + 1
if nums[left_index] == target:
return left_index
return -1
"""
This code got better score than last code!
Runtime: 36 ms, faster than 89.33% of Python3 online submissions for Search in Rotated Sorted Array.
Memory Usage: 14.8 MB, less than 22.57% of Python3 online submissions for Search in Rotated Sorted Array.
"""
class Solution:
def search(self, nums: List[int], target: int) -> int:
#def search(self, nums: list, target: int) -> int:
left_index = 0
right_index = len(nums) -1
while left_index < right_index:
mid_index = (left_index + right_index) // 2
left_val = nums[left_index]
right_val = nums[right_index]
mid_val = nums[mid_index]
if mid_val == target:
return mid_index
if mid_val <= left_val: # pivot is on left part:
if target > mid_val and target <= right_val: # target is in range
left_index = mid_index + 1
else: # target <= right_val
right_index = mid_index -1
if mid_val >= left_val: # the pivot is not in this range:
if target >= left_val and target < mid_val:
right_index = mid_index -1
else:
left_index = mid_index + 1
if nums[left_index] == target:
return left_index
return -1