63 Search rotation sorted Array II

Source: Internet
Author: User

Original title URL: https://www.lintcode.com/problem/search-in-rotated-sorted-array-ii/description

Describe

Follow the "Search rotation sorted array", what if there are duplicate elements?

Does it affect the complexity of the run time?

How does it affect?

Why is it affected?

Write a function that determines whether a given target value appears in the array.

Have you ever encountered this problem in a real interview?is aSample Example

Given [3,4,4,5,7,0,1,2] and target=4, return True

Label binary sorting array idea: The method is similar to the search sorted array, just a duplicate judgment, if the left element and mid element is the same, the elements of the left~mid part must be the same, you can skip this part to Judge Mid+1, namely left++; element repetition can affect the complexity of time, because repetition is not binary lookup, but with a distance of 1 steps, the complexity of time becomes larger. AC Code:
classSolution { Public:    /** * @param a:an integer ratated sorted array and duplicates are allowed * @param target:an integer * @re Turn:a Boolean*/    BOOLSearch (vector<int> &a,inttarget) {        //Write your code here        if(A.empty ()) {return false; }    intSize=a.size (); intleft=0, right=size-1, Mid;  while(left<=Right ) {Mid= (left+right)/2; if(a[mid]==target) {            return true; }        if(a[mid]>A[left]) {            if(a[left]<=target&&target<A[mid]) { Right=mid-1; }            Else{ Left=mid+1; }        }        Else if(a[mid]<A[left])if(target>a[mid]&&target<=A[right]) { Left=mid+1; }            Else{ Right=mid-1; }        }        Else//equal-time order lookup;{ Left++; }    }    return false; }};

Mark A resource: Algorithm summary-two points search and rotate sorted array

63 Search rotation 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.