Leetcode Oj:search in rotated Sorted array II (flipping the lookup of sorted arrays)

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.

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)

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.