标签:blog io for ar art cti div amp
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.
出现相等元素之后会出现一种特殊退化的情况,即中间元素等于start,end的元素,这样无法判断左边或者右边是否出现突增,增减,所以这种情况下必须递归两边,算法退化成线性比较。
1st (3 tries)
class Solution
{
public:
bool search(int A[], int n, int target)
{
if(A[0] < A[n-1])
return binary_search(A,0,n-1,target);
else
return ordinary_search(A,0,n-1,target);
}
bool binary_search(int A[],int start,int end,int target)
{
if(start > end)
return false;
int mid = (start + end)/2;
if(A[mid] == target)
return true;
if(A[mid] > target)
{
return binary_search(A,start,mid-1,target);
}
else
{
return binary_search(A,mid+1,end,target);
}
}
bool ordinary_search(int A[],int start,int end,int target)
{
if(start > end)
return false;
int mid = (start + end)/2;
if(A[mid] == target)
return true;
/**this is new**//**test case 1,3,1,1,1 or 1,1,1,3,1 无法断定3的具体位置**/
if(A[start] == A[mid] && A[mid] == A[end])
{
return ordinary_search(A,start,mid-1,target)||ordinary_search(A,mid+1,end,target);
}
else if(A[start] <= A[mid])
{
if(target <= A[mid] && target >= A[start])
{
return binary_search(A,start,mid-1,target);
}
else
{
return ordinary_search(A,mid+1,end,target);
}
}
else
{
if(target >= A[mid] && target <= A[end])
{
return binary_search(A,mid+1,end,target);
}
else
{
return ordinary_search(A,start,mid-1,target);
}
}
}
};
2nd (3 tries)
class Solution {
public:
bool search(int A[], int n, int target) {
//if exists equal so don‘t know whether left or right is available???
return search(A,0,n-1,target);
}
bool search(int A[], int start, int end, int target) {
if(start > end)
return false;
int mid = start + (end - start)/2;
if( A[mid] == target )
return true;
else if( A[mid] > target ) {
//two side???
if( A[start] == A[end] ) {
return search(A,start,mid-1,target) || search(A,mid+1,end,target);
}
else {
//no rotate,sorted array!!!
if( A[start] < A[end] ) {
return bsearch(A,start,end,target);
}
// A[start] > A[end]!!!
else {
if( A[mid] < A[start] ) {
return search(A,start,mid-1,target);
}
//A[mid] >= A[start]
else {
if(target >= A[start]) {
return bsearch(A,start,mid-1,target);
}
else {
return search(A,mid+1,end,target);
}
}
}
}
}
else {
if( A[start] == A[end] ) {
return search(A,start,mid-1,target) || search(A,mid+1,end,target);
}
else {
//no rotate,sorted array!!!
if( A[start] < A[end] ) {
return bsearch(A,start,end,target);
}
// A[start] > A[end]!!!
else {
if( A[mid] < A[start] ) {
if(target <= A[end]) {
return bsearch(A,mid+1,end,target);
}
else {
return search(A,start,mid-1,target);
}
}
//A[mid] >= A[start]
else {
return search(A,mid+1,end,target);
}
}
}
}
}
//binary search!!!
bool bsearch(int A[], int start, int end, int target) {
while(start <= end) {
int mid = start + (end-start)/2;
if(A[mid] == target)
return true;
else if(A[mid] > target) {
end = mid-1;
}
else {
start = mid+1;
}
}
return false;
}
};
【Leetcode】Search in Rotated Sorted Array II,布布扣,bubuko.com
【Leetcode】Search in Rotated Sorted Array II
标签:blog io for ar art cti div amp
原文地址:http://www.cnblogs.com/weixliu/p/3924328.html