Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
//二分查找法,主要要确定好左右边界
class Solution {
public:
int removeDuplicates(int *a, int len, int target)
{
int start = 0;
int end = len - 1;
while (start != end)
{
int mid = (start + end) / 2;
if (a[mid] == target)
{
return mid;
}
if (a[start] <= a[mid])
{
if (a[start] < target&&target < a[mid])
end = mid;
else
start = mid + 1;
}
else
{
if (a[start] > target&&target < a[mid])
end = mid;
else
start = mid + 1;
}
}
}
};
int main()
{
int a[] = { 3,2,1,5,4 };
Solution b;
int length = b.removeDuplicates(a, 5,1);
printf("%d \n",length);
return 0;
}