Search in Rotated Sorted Array II

跟1 差不多,就是多了个一旦相等,就挪边缘的条件, 参考http://blog.csdn.net/linhuanmars/article/details/20588511

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

 

posted @ 2015-04-17 10:11  世界到处都是小星星  阅读(124)  评论(0编辑  收藏  举报