Leetcode 7. Searching for search in rotated Sorted array II after an ordered repeatable array rotation

Source: Internet
Author: User

Search in rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.

Problem Solving Ideas:
The subject is based on Leetcode 6. Searches the search in rotated Sorted array after an ordered array is rotated, allowing duplicates to exist in an ordered array.
the difference between the previous question (which is not allowed to repeat) is that
NUMS[M] > Nums[l]: (L, M-1) monocytogenes
NUMS[M] <= Nums[l]: (m+1, R) not necessarily monocytogenes , because {1,3,1,1,1} or {1,1,1,3,1}

At this point, you can see the above conditions separately
NUMS[M] < Nums[l]: (m+1, R) must monocytogenes
Num[m] = = Nums[l]: Will l+1, re-recursive calculation ( when [L, R], will r-1)

classSolution { Public://nums array boundary is [L,r]    BOOLSEARCHR ( vector<int>& Nums,intLintRintTarget) {if(R <= L)return false;intm = (l+r)/2;if(Nums[m] = = target)return true;if(Nums[l] < nums[m]) {if(Target >= nums[l] && target < nums[m])returnSEARCHR (Nums, L, M, target);Else                returnSEARCHR (Nums, m+1, R, Target); }Else if(Nums[l] > Nums[m]) {if(Target > Nums[m] && target <= nums[r-1])returnSEARCHR (Nums, m+1, R, Target);Else                returnSEARCHR (Nums, L, M, target); }Else{returnSEARCHR (Nums, ++l, R, Target); }    }BOOLSearch vector<int>& Nums,intTarget) {returnSEARCHR (Nums,0, Nums.size (), target); }};

Leetcode 7. Searching for search in rotated Sorted array II after an ordered repeatable array rotation

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.