旋转数组的查找-经典

https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description

很好的很经典的题目。今天复习了一下。之前的思路虽然有了,但是对于相等的数字的处理很复杂,容易出错。今天看到了一个很好的解法。

https://discuss.leetcode.com/topic/3538/concise-o-log-n-binary-search-solution

思路是先找出最小的数字的下标,通过 mid > hi,就low = mid+1,否则 hi=mid,来找出。

然后二分查找的时候,用的是偏移过的实际下标。

注意,下面这个不能有重复。有重复的,在今天后面的帖子里面描述。

http://www.cnblogs.com/charlesblc/p/6451503.html

整体代码:

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;
    }
};

当然了,还有常规的做法,就是这样的。

考虑问题的时候,也不要太过复杂化了。

public int search(int[] A, int target) {
    int lo = 0;
    int hi = A.length - 1;
    while (lo < hi) {
        int mid = (lo + hi) / 2;
        if (A[mid] == target) return mid;
        
        if (A[lo] <= A[mid]) {
            if (target >= A[lo] && target < A[mid]) {
                hi = mid - 1;
            } else {
                lo = mid + 1;
            }
        } else {
            if (target > A[mid] && target <= A[hi]) {
                lo = mid + 1;
            } else {
                hi = mid - 1;
            }
        }
    }
    return A[lo] == target ? lo : -1;
}
原文地址:https://www.cnblogs.com/charlesblc/p/6444027.html