1. 程式人生 > >【LeetCode】Search in Rotated Sorted Array 解題報告

【LeetCode】Search in Rotated Sorted Array 解題報告

【題目】

Suppose a sorted array 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在左邊還是在右邊,其中第一種情況還可以直接判斷target有可能不在陣列範圍內。

【Java程式碼】

public class Solution {
    public int search(int[] A, int target) {
        int len = A.length;
        if (len == 0) return -1;
        return binarySearch(A, 0, len-1, target);
    }
    
    public int binarySearch(int[] A, int left, int right, int target) {
        if (left > right) return -1;
        
        int mid = (left + right) / 2;
        if (A[left] == target) return left;
        if (A[mid] == target) return mid;
        if (A[right] == target) return right;
        
        //圖示情況一
        if (A[left] < A[right]) { 
            if (target < A[left] || target > A[right]) {    //target不在陣列範圍內
                return -1;
            } else if (target < A[mid]) {                   //target在左邊
                return binarySearch(A, left+1, mid-1, target);
            } else {                                        //target在右邊
                return binarySearch(A, mid+1, right-1, target);
            }
        } 
        //圖示情況二
        else if (A[left] < A[mid]) { 
            if (target > A[left] && target < A[mid]) {      //target在左邊
                return binarySearch(A, left+1, mid-1, target);
            } else {                                        //target在右邊
                return binarySearch(A, mid+1, right-1, target);
            }
        } 
        //圖示情況三
        else { 
            if (target > A[mid] && target < A[right]) {     //target在右邊
                return binarySearch(A, mid+1, right-1, target);
            } else{                                         //target在左邊
                return binarySearch(A, left+1, mid-1, target);
            }
        }
    }
}

下面是參考網上的思路,其中if (target == A[left]) 和 if (target == A[right]) 兩個判斷是我自己加上的,因為加上這兩個判斷後,下面分情況討論時就不用考慮target等於邊界的情況了。
public class Solution {
    public int search(int[] A, int target) {
        int len = A.length;
        if (len == 0) {
            return -1;
        } else if (len == 1) {
            return target==A[0] ? 0 : -1;
        }
        
        int left = 0, right = len-1;
        while (left < right) {
            int mid = left + (right-left)/2;
            if (target == A[mid]) {
                return mid;
            } else if (target == A[left]) {
                return left;
            } else if (target == A[right]) {
                return right;
            }
            
            //第一種情況中,target不在陣列範圍內
            if (A[left]<A[right] && (target<A[left] || target>A[right])) {
                return -1;
            }
            
            //第一、二種情況的左邊,即連續上升的左邊,且target在這段內
            if (A[left]<A[mid] && target>A[left] && target<A[mid]) {
                right = mid - 1;
                continue;
            }
            
            //第一、三種情況的右邊,即連續上升的右邊,且target在這段內
            if (A[mid]<A[right] && target>A[mid] && target<A[right]) {
                left = mid + 1;
                continue;
            }
            
            //如果上面情況都不滿足,那麼可能在第二種情況的右邊
            if (A[mid] > A[right]) {
                left = mid + 1;
                continue;
            }
            
            //如果上面情況都不滿足,第三種情況的左邊
            if (A[left] > A[mid]) {
                right = mid - 1;
                continue;
            }
        }
        
        return -1;
    }
}

個人感覺還是自己那樣寫思路比較清晰。

2015/3/30更新

public class Solution {
    public int search(int[] A, int target) {
        int l = 0;
        int r = A.length - 1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (target == A[mid]) return mid;
            if (A[l] <= A[r]) {
                if (target < A[mid]) r = mid - 1;
                else l = mid + 1;
            } else if (A[l] <= A[mid]) {
                if (target > A[mid] || target < A[l]) l = mid + 1;
                else r = mid - 1;
            } else {
                if (target < A[mid] || target > A[r]) r = mid - 1;
                else l = mid + 1;
            }
        }
        return -1;
    }
}