美文网首页
Search in Rotated Sorted Array

Search in Rotated Sorted Array

作者: BigBig_Fish | 来源:发表于2017-07-13 20:40 被阅读0次

    Suppose an array sorted in ascending order 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.

    题目分析

    在一个升序数组中寻找一个target的位置,这个升序数组可能经过了轮换。一开始以为是找出轮换前的位置,后来发现就是找轮转后的位置。利用二分法,先找到最小值得坐标,然后通过坐标转换进行查找。

    代码

    class Solution {
    public:
        int search(int A[], int n, int target) {
            int lo=0,hi=n-1;
            // find the index of the smallest value using binary search.
            // Loop will terminate since mid < hi, and lo or hi will shrink by at least 1.
            // Proof by contradiction that mid < hi: if mid==hi, then lo==hi and loop would have been terminated.
            while(lo<hi){
                int mid=(lo+hi)/2;
                if(A[mid]>A[hi]) lo=mid+1;
                else hi=mid;
            }
            // lo==hi is the index of the smallest value and also the number of places rotated.
            int rot=lo;
            lo=0;hi=n-1;
            // The usual binary search and accounting for rotation.
            while(lo<=hi){
                int mid=(lo+hi)/2;
                int realmid=(mid+rot)%n;
                if(A[realmid]==target)return realmid;
                if(A[realmid]<target)lo=mid+1;
                else hi=mid-1;
            }
            return -1;
        }
    };
    

    其实也可以把这个数组转换成原始数组,然后利用二分查找搜索。

    Search in Rotated Sorted Array II

    Suppose an array sorted in ascending order 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).

    Write a function to determine if a given target is in the array.

    The array may contain duplicates.

    代码

    核心还是二分法,判断mid和low的关系,如果mid>low则说明在这个范围内是递增的,反之在mid到high之间的范围是递增的,然后继续二分搜索就行。

    class Solution {
    public:
        bool search(vector<int>& nums, int target) {
            int left = 0, right =  nums.size()-1, mid;
            
            while(left<=right)
            {
                mid = (left + right) >> 1;
                if(nums[mid] == target) return true;
    
                // the only difference from the first one, trickly case, just updat left and right
                if( (nums[left] == nums[mid]) && (nums[right] == nums[mid]) ) {++left; --right;}
    
                else if(nums[left] <= nums[mid])
                {
                    if( (nums[left]<=target) && (nums[mid] > target) ) right = mid-1;
                    else left = mid + 1; 
                }
                else
                {
                    if((nums[mid] < target) &&  (nums[right] >= target) ) left = mid+1;
                    else right = mid-1;
                }
            }
            return false;
        }
    };
    

    相关文章

      网友评论

          本文标题:Search in Rotated Sorted Array

          本文链接:https://www.haomeiwen.com/subject/fukrhxtx.html