[Leetcode] Search in rotated Sorted Array II

Source: Internet
Author: User

For those who has already solved Search in rotated Sorted Array, this problem can be solved similarly using code s for this problem and simply adding codes to skip the duplicates.

For Search in rotated Sorted arrays, I post solutions in C/c++/python here (C and C + + only needs one lines).

Now, based in the above codes, you can solve this problem by simply adding both lines to skip duplicates both starting from Left and right.

1 classSolution {2  Public: 3     BOOLSearch (vector<int>& Nums,inttarget) {4         intL =0, r = nums.size ()-1;5          while(L <=r) {6              while(L < r && Nums[l] = = Nums[l +1]) l++;//Skip duplicates from the left7              while(R > L && nums[r] = = Nums[r-1]) r--;//Skip duplicates from the right8             intMid = (L + r)/2;9             if(Nums[mid] = = target)return true; Ten             if(Nums[mid] >target) { One                 if(Nums[l] <= target | | nums[mid] < NUMS[L]) R = mid-1; A                 ElseL = mid +1; -             } -             Else { the                 if(Nums[l] > Target | | nums[mid] >= nums[l]) L = mid +1; -                 ElseR = Mid-1; -             } -         } +         return false; -     } +};

[Leetcode] 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.