Original title URL: https://www.lintcode.com/problem/search-in-rotated-sorted-array-ii/description
Describe
Follow the "Search rotation sorted array", what if there are duplicate elements?
Does it affect the complexity of the run time?
How does it affect?
Why is it affected?
Write a function that determines whether a given target value appears in the array.
Have you ever encountered this problem in a real interview?is aSample Example
Given [3,4,4,5,7,0,1,2] and target=4, return True
Label binary sorting array idea: The method is similar to the search sorted array, just a duplicate judgment, if the left element and mid element is the same, the elements of the left~mid part must be the same, you can skip this part to Judge Mid+1, namely left++; element repetition can affect the complexity of time, because repetition is not binary lookup, but with a distance of 1 steps, the complexity of time becomes larger. AC Code:
classSolution { Public: /** * @param a:an integer ratated sorted array and duplicates are allowed * @param target:an integer * @re Turn:a Boolean*/ BOOLSearch (vector<int> &a,inttarget) { //Write your code here if(A.empty ()) {return false; } intSize=a.size (); intleft=0, right=size-1, Mid; while(left<=Right ) {Mid= (left+right)/2; if(a[mid]==target) { return true; } if(a[mid]>A[left]) { if(a[left]<=target&&target<A[mid]) { Right=mid-1; } Else{ Left=mid+1; } } Else if(a[mid]<A[left])if(target>a[mid]&&target<=A[right]) { Left=mid+1; } Else{ Right=mid-1; } } Else//equal-time order lookup;{ Left++; } } return false; }};
Mark A resource: Algorithm summary-two points search and rotate sorted array
63 Search rotation sorted Array II