题目要求:
二分查找被旋转的有序数组(允许数组元素重复),找到返回true,如果不存在待查找数便返回false。
例子:
特殊情况如:
(1、4、1、1、1、1、1) 查找3返回true;
(1、1、1、1、1、4、1) 查找3返回true;
代码:
#include<stdafx.h>
#include <iostream>
using namespace std;
class Solution {
public:
bool searchInsert(int A[], int n, int target) {
int low=0;
int cur=(n-1)/2;
int high=n-1;
while(low<=high)
{
int cur= (low+high)/2;
if(target == A[cur])
return true;
if(A[cur]<=A[high])
{
if(target<=A[high] && target>A[cur])
low = cur+1;
else
high = cur-1;
}
else
{
if(target>=A[low] && target<A[cur])
high = cur-1;
else
low = cur+1;
}
}
return false;
}
};
void main()
{
int A[9]={4,4,5,6,7,0,0,1,2};
Solution s;
cout<<s.searchInsert(A,9,4)<<endl;
getchar();
}