[Leetcode]26. Search in rotated Array II rotate array Find 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.

Solution: With the rotating array to find I, the subject also uses a binary search, only in Nums[mid] and Nums[right] (or nums[right]) compared if the two are equal left++ (or right--) can be. Nums[mid] compared to nums[left] code:

classSolution { Public:    intSearch (vector<int>& Nums,inttarget) {        intn =nums.size (); BOOLres =false; intleft =0, right = N-1;  while(Left <=Right ) {            intMid = (left + right) >>1); if(target = =Nums[mid]) {Res=true;  Break; }            Else if(Nums[mid] > Nums[left])//first half ordered            {                if(Target >= Nums[left] && Target <Nums[mid]) right= Mid-1; Else Left= Mid +1; }            Else if(Nums[mid] < Nums[left])//The second part is ordered            {                if(Target > Nums[mid] && target <=Nums[right]) left= Mid +1; Else Right= Mid-1; }            Else //Nums[mid]==nums[left], including the presence of mid=left and the existence of duplicate values of two casesleft++; }        returnRes; }};

Nums[mid] compared to nums[right] code:

classSolution { Public:    intSearch (vector<int>& Nums,inttarget) {        intn =nums.size (); BOOLres =false; intleft =0, right = N-1;  while(Left <=Right ) {            intMid = (left + right) >>1; if(target = =Nums[mid]) {Res=true;  Break; }            Else if(Nums[mid] < nums[right])//The second part is ordered            {                if(Target > Nums[mid] && target <=Nums[right]) left= Mid +1; Else Right= Mid-1; }            Else if(Nums[mid] > Nums[right])//first half ordered            {                if(Target >= Nums[left] && Target <Nums[mid]) right= Mid-1; Else Left= Mid +1; }            Else //cases where duplicate values are presentright--; }            returnRes; }};

[Leetcode]26. Search in rotated Array II rotate array Find 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.