2014年5月10日星期六

Binary Search in Array Solution - Search in Rotated Sorted Array II with duplicates

The idea is still binary search, but take more resources. All pair will be searched.

public class Solution {
    public boolean search(int[] A, int target) {
        if(A == null || A.length == 0){
            return false;
        }
        return searchOps(A, target, 0, A.length - 1);
    }
   
    private boolean searchOps(int[] A, int target, int start, int end){

        if(A[(start + end)/2] == target || A[start] == target || A[end] == target){
            return true;
        }
        if(start == end || end - start < 2){
            return false;
        }
        int midIndex = (start + end) / 2;
        return searchOps(A, target, start, midIndex - 1) || searchOps(A, target, midIndex + 1, end);
       
        //1131,13111
    }
}//duplicates will affect the edge case for narrow sides process, like 1112311

没有评论: