码迷,mamicode.com
首页 > 其他好文 > 详细

Search in Rotated Sorted Array

时间:2014-10-02 12:50:32      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   for   

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

ie: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, return its index,else return -1, there is no duplicate in the array

首先解决一个问题:

bubuko.com,布布扣
#include<iostream>
using namespace std;

int sizeofarr(int arr[])
{
    return sizeof(arr)/sizeof(arr[0]);
}

int main()
{
    int arr[4]={1,2,3,4};
    cout<<sizeofarr(arr)<<endl;
    cout<<sizeof(int*)<<endl;

    cout<<sizeof(arr)/sizeof(arr[0])<<endl;
    cout<<sizeof(int)<<endl;

    return 0;
}
View Code

输出为:2,8,4,4,注意到sizeof(arr)/sizeof(arr[0])在函数sizeofarr中返回值并不是4,而是2=sizeof(int*)/sizeof(int);在c++中STL中vector是一个不错的替代;

对于上面的问题:

思路:采用二分查找,问题的关键在于分类边界的确定,根据A[left]与A[mid]的关系做为分类依据,在进行细分

code:

bubuko.com,布布扣
class Solution {
public:
    int search(int A[], int n, int target) {
            
        int left=0;
        int last=n;
        
       //此处也可以写做:while(left!=last),但是若为while(left<=last)
      //则对于如[1],0的情况则不能处理
        while(left<last)
        {
            const int mid=(left+last)/2;
    
            if(A[mid]==target)
                return mid;
            else if(A[mid]>=A[left])
            {
                if(target>=A[left]&&target<A[mid])
                    last=mid;
                else
                    left=mid+1;
            }
            else
            {
                if(target>A[mid]&&target<=A[last-1])
                    left=mid+1;
                else
                    last=mid;
            }
        }

        return -1;
    }
};
View Code

因为判断中使用:target<=A[last-1],所以边界中使用last=mid,left=mid+1;

当然对此问题也可以使用递归的方法,思路同上,只需稍加修改;

code:

bubuko.com,布布扣
class Solution {
public:
    int search(int A[], int n, int target) 
    {
        return searchA(A,target,0,n);
    }
    
    int searchA(int A[],int target,int left,int last)
    {
        if(left>=last)
            return -1;
    
        int mid=(left+last)/2;
    
        if(A[mid]==target)
            return mid;
        if(A[mid]>=A[left])
        {
            if(target>=A[left]&&target<A[mid])
                searchA(A,target,left,mid);
            else
                searchA(A,target,mid+1,last);
        }
        else
        {
            if(target>A[mid]&&target<=A[last-1])
                searchA(A,target,mid+1,last);
            else
                searchA(A,target,left,mid);
        }
    }
};
View Code

若在上述问题中,duplicates are allowed, how to deal with the problem?

可对情况进行细分,若在判定条件target>=A[left]&&target<A[mid] 不满足时,若A[mid]==A[left],则++left,else left=mid+1

对[1,3,1,1],3情况的分析可方便理解;

code:

bubuko.com,布布扣
class Solution {
public:
    bool search(int A[], int n, int target) 
    {
        int left=0;
        int last=n;

        while(left<last)
        {
            const int mid=(left+last)/2;
    
            if(A[mid]==target)
                return true;
            else if(A[mid]>=A[left])
            {
                if(target>=A[left]&&target<A[mid])
                    last=mid;
                //there are two cases to deal with
                else
                {
                    if(A[mid]==A[left])
                        ++left;
                    else
                        left=mid+1;
                }
            }
            else
            {
                if(target>A[mid]&&target<=A[last-1])
                    left=mid+1;
                else
                    last=mid;
            }
        }
    
        return false;
        }
    };
View Code

 

Search in Rotated Sorted Array

标签:style   blog   http   color   io   os   使用   ar   for   

原文地址:http://www.cnblogs.com/chengyuz/p/4003248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!