-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search in Rotated Sorted Array II.cpp
53 lines (51 loc) · 1.27 KB
/
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
class Solution {
public:
int minIndex(vector<int>&nums)
{
int i=0;
int j=nums.size()-1;
while(i!=j)
{
int mid=(i+j)/2;
if(nums[i]==nums[j]&&nums[j]==nums[mid])//这种情况二分法搞不了
{
for(int k=i;k<j;k++)
{
if(nums[k]>nums[k+1])
return k+1;
}
return i;
}
if(nums[mid]>nums[j])i=mid+1;
else j=mid;
if(i==j-1)
{
return nums[i]>nums[j]?j:i;
}
}
return i;
}
bool Find(vector<int>& nums,int start,int end,int target)//用二分法找到一个递增数组中的target
{
if(start>end)return false;
while(start<=end)
{
int mid=(start+end)/2;
if(nums[mid]==target)return true;
else if(nums[mid]<target)start=mid+1;
else end=mid-1;
}
return false;
}
bool search(vector<int>& nums, int target) {
if(nums.size()<=0)return -1;
int i=0;
int j=nums.size()-1;
int key=minIndex(nums);//这是最小值的下标
bool a1=0;
bool a2=0;
a1=Find(nums,0,key-1,target);
a2=Find(nums,key,nums.size()-1,target);
return a1||a2;
}
};