zoukankan      html  css  js  c++  java
  • 旋转数组的查找-经典

    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;
    }
  • 相关阅读:
    Linux 共享库
    使用Visual Studio(VS)开发Qt程序代码提示功能的实现(转)
    ZOJ 3469 Food Delivery(区间DP)
    POJ 2955 Brackets (区间DP)
    HDU 3555 Bomb(数位DP)
    HDU 2089 不要62(数位DP)
    UESTC 1307 windy数(数位DP)
    HDU 4352 XHXJ's LIS(数位DP)
    POJ 3252 Round Numbers(数位DP)
    HDU 2476 String painter (区间DP)
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6444027.html
Copyright © 2011-2022 走看看