Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
算法思想:
和Search in Rotated Sorted Array
类似,只是需要对相等的元素进行单独处理
class Solution {
public:
bool search(int A[], int n, int target) {
int left=0,right=n-1;
while(left<=right){
int mid=(left+right)/2;
if(A[left]<target&&target<A[mid])right=mid-1;
else if(A[mid]<target&&target<A[right])left=mid+1;
else{
if(A[left]!=target)left++;
else return true;
if(A[right]!=target)right--;
else return true;
}
}
return false;
}
};
旋转有序数组搜索II
本文介绍了一种在允许重复元素的旋转有序数组中查找特定目标值的算法。该算法通过调整二分查找策略来处理重复元素的问题,并给出了具体的实现代码。

被折叠的 条评论
为什么被折叠?



