-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path81.search-in-rotated-sorted-array-ii.cpp
77 lines (72 loc) · 2 KB
/
81.search-in-rotated-sorted-array-ii.cpp
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
/*
* @lc app=leetcode id=81 lang=cpp
*
* [81] Search in Rotated Sorted Array II
*
* https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/description/
*
* algorithms
* Medium (36.00%)
* Total Accepted: 43.1K
* Total Submissions: 119.6K
* Testcase Example: '[2,5,6,0,0,1,2]\n0'
*
* 假设按照升序排序的数组在预先未知的某个点上进行了旋转。
*
* ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] )。
*
* 编写一个函数来判断给定的目标值是否存在于数组中。若存在返回 true,否则返回 false。
*
* 示例 1:
*
* 输入: nums = [2,5,6,0,0,1,2], target = 0
* 输出: true
*
*
* 示例 2:
*
* 输入: nums = [2,5,6,0,0,1,2], target = 3
* 输出: false
*
* 进阶:
*
*
* 这是 搜索旋转排序数组 的延伸题目,本题中的 nums 可能包含重复元素。
* 这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
*
*
*/
class Solution {
public:
bool binarySearch(vector<int>& nums, int l, int r, int target) {
while(l <= r) {
int mid = (l + r) >> 1;
if (nums[mid] == target) return true;
if (nums[mid] < target) l += 1;
else r -= 1;
}
return false;
}
int find(vector<int>& nums) {
int n = nums.size();
int l = 0, r = n - 1;
int edge = nums[0];
while(l < n && nums[l] == edge) l ++;
if (l == n) return 0;
while(r >= 0 && nums[r] == edge) r --;
if (r == -1) return 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (nums[mid] >= nums[0]) l = mid + 1;
else r = mid - 1;
}
return l - 1;
}
bool search(vector<int>& nums, int target) {
int n = nums.size();
if (n == 0) return false;
int split = find(nums);
printf("split: %d\n", split);
return binarySearch(nums, 0, split, target) || binarySearch(nums, split+1, n-1, target);
}
};