forked from HuberTRoy/leetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchInRotatedSortedArrayII.py
103 lines (73 loc) · 2.58 KB
/
SearchInRotatedSortedArrayII.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
"""
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
search in rotated sorted array 变形版本:
与之前的区别是有重复,重复的值会造成什么样的影响呢?
1, 1, 1, 1, 1, 1, 2, 1, 1
在这个例子中
旋转的点是 1, 如果再用之前的方法,nums[0] 是没办法保证比旋转的点之后的元素都大的,
循环之后的mid是1,不大于nums[0] 不大于 mid 的情况中会把hi变为mid这样就会造成之后的数据丢失。
其他的情况可以与 I 一样。
我想到的解决方法:
1. 可以把与 nums[0] 相同的一直吞并。 这里用的方法是一直迭代。
总时间复杂度的话最差会有 O(n + log n)。
测试地址:
https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/
beat 89% 24ms
平均24 - 28 ms。
"""
class Solution(object):
def find_rotate(self, nums):
target = nums[0]
lo = 1
for i in range(1, len(nums)):
if nums[i] == target:
lo += 1
else:
break
hi = len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] > target:
lo = mid + 1
else:
hi = mid
return lo
def bi_search(self, nums, target, lo, hi):
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] == target:
return mid
if nums[mid] > target:
hi = mid
else:
lo = mid + 1
return -1
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if not nums:
return False
rotate_index = self.find_rotate(nums)
lo = 0
hi = rotate_index
one = self.bi_search(nums, target, lo, hi)
if one != -1:
return True
two = self.bi_search(nums, target, hi, len(nums))
if two != -1:
return True
return False