首页 > 代码库 > Search in Rotated Sorted Array II

Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

答案

public class Solution {
    public boolean search(int[] A, int target) {
        if(A==null)
        {
            return false;
        }
        int left=0;
        int right=A.length-1;
        int middle;
        while(left<=right)
        {
            middle=left+(right-left)/2;
            if(A[middle]==target)
            {
                return true;
            }
            if(A[left]==A[right])
            {
                left++;
                continue;
            }
            if(A[left]<=A[middle])
            {
                if(A[left]<=target&&target<=A[middle])
                {
                    right=middle-1;
                }
                else
                {
                    left=middle+1;
                }
            }
            else
            {
                if(A[middle]<target&&target<=A[right])
                {
                    left=middle+1;
                }
                else
                {
                    right=middle-1;
                }
            }
        }
        return false;
    }
}


Search in Rotated Sorted Array II