leetcode - 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) {
		int left = 0,right = n - 1;
		while(left <= right)
		{
			int mid = left + (right - left) / 2;
			if(A[left] < target && target < A[mid]) right = mid - 1;
			if(A[mid] < target && target < A[right])left  = mid + 1;
			else
			{
				if(A[left] != target) left++;
				else return true;
				if(A[right]!= target) right--;
				else return true;
			}
		}
		return false;
    }
};


posted @ 2015-12-22 21:56  lcchuguo  阅读(159)  评论(0编辑  收藏  举报