[Leedcode 81] Search in rotated Sorted Array II

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.