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.


class Solution {
public:
    bool search(int A[], int n, int target) {
        if (n < 1)
        {
            return false;
        }

        int left = 0;
        int right = n-1;
        while (left <= right)
        {
            int mid = left + (right-left)/2;
            if (A[mid] == target)
            {
                return true;
            }
            else
            {
                for (int i = mid+1; i <= right; i++)
                {
                    if (A[i] == target)
                    {
                        return true;
                    }
                }
                right = mid - 1;
            }
        }

        return false;
    }
};


你可能感兴趣的:(Search in Rotated Sorted Array II)