Describe
Follow up for ' Search in rotated Sorted Array ': What if duplicates is allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given the target was in the array.
Analysis
Allow repeating elements, if a[m]>=a[l] in the previous question, then [L,m] is the assumption that the increment sequence cannot be established, than
such as [1,3,1,1,1].
If A[M]>=A[L] cannot be determined to increment, split it into two conditions:
? If A[m]>a[l], the interval [l,m] must increment
? If A[M]==A[L] can not be determined, then l++, look down one step.
Two-point Search
Code
1 //Leetcode, Search in rotated Sorted Array II2 //time complexity O (log n), Space complexity O (1)3 classSolution {4 Public Static BooleanSearchintA[],intNinttarget) {5 intFirst = 0, last =N;6 while(First! =Last ) {7 intMid = (first + last)/2;8 if(A[mid] = =target)9 return true;Ten if(A[first] <A[mid]) { One if(A[first] <= target && Target <A[mid]) ALast =mid; - Else -First = mid + 1; the}Else if(A[first] >A[mid]) { - if(A[mid] <= target && target <= a[last-1]) -First = mid + 1; - Else +Last =mid; -}Else + //Skip duplicate one, a[start] = = A[mid] Afirst++; at } - return false; - } -}
Search in rotated Sorted Array II leetcode Java