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.
Public classSolution { Public BooleanSearchint[] Nums,inttarget) { //pay attention to use the law of the subject to judge://Nums[0] is the focus, it is the minimum value of the first half, the maximum value of the second half, so mid needs to determine the size with it, and then to distinguish the range in which the mid falls//The median and start judgment, if greater than start, indicates that start to mid is an increment sequence, as long as the target is determined in this range//if it is less than start, the mid to end is an increment sequence, so long as the target is determined to be in this range//if the median is equal to start (implied Nums[start] is not equal to target), then start++ at this time; returnFindsearch (nums,0,nums.length-1, Target); } Public BooleanFindsearch (int[] Nums,intStartintEndinttarget) { if(start>end) { return false; } intMid= (start+end)/2; if(Nums[mid]==target)return true; if(nums[mid]>Nums[start]) { if(target<nums[mid]&&target>=Nums[start]) {End=mid-1; }Else{Start=mid+1; } }Else if(nums[mid]<Nums[start]) { if(target>nums[mid]&&target<=Nums[end]) {Start=mid+1; }Else{End=mid-1; } }Elsestart++;//////// returnFindsearch (Nums,start,end,target); }}
[Leedcode 81] Search in rotated Sorted Array II