Follow to "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.
This is not the same as the previous one is that there may be repeated numbers, then the judgment should be noted, the encounter Start<=mid does not necessarily indicate that the current interval [start, mid] is incremented. This time should be + + to confirm, the code is as follows:
1 //Leetcode, Search in rotated Sorted Array II2 //time complexity O (n), Spatial complexity O (1)3 classSolution {4 Public:5 BOOLSearch (vector<int>&nums,inttarget) {6 intFirst =0, last = Nums.size ()-1;7 while(First <=Last ) {8 Const intMid = (first + last)/2;9 if(Nums[mid] = =target)Ten return true; One if(Nums[first] <Nums[mid]) { A if(Nums[first] <= target && Target <Nums[mid]) -Last = mid-1; - Else theFirst = mid +1; -}Else if(Nums[first] >Nums[mid]) { - if(Nums[mid] < target && Target <=Nums[last]) -First = mid +1; + Else -Last =mid; +}Else A //Skip Duplicate One atfirst++; - } - return false; - } -};
Leetcode Oj:search in rotated Sorted array II (flipping the lookup of sorted arrays)