[Leetcode] [Java] Search in rotated Sorted Array II

Source: Internet
Author: User

Title:

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.

Test Instructions:

Along with the question "Search in rotated Sorted Array":

What if the repeating element is allowed?

Write a function to determine whether the target value is in the array.


Algorithm Analysis:

The question I started is to go through it all over again, so I don't have to find the target element, I think it's not that simple.

But this time complexity is O (n), although the same can be AC, but not our pursuit of the algorithm ha.

reference http://blog.csdn.net/linhuanmars/article/ details/20588511 

This question is binary search search Insert position Variant, thinking in the Search in rotated Sorted array Search in rotated Sorted array The only difference is that the elements in this topic are duplicated. However, because of this condition, there are more complicated cases, even affecting the time complexity of the algorithm. It turns out that we rely on the relationship between the middle and the edge elements to determine which half is unaffected by rotate and still orderly. And now because of the repetition, if we encounter the middle and edge of the same situation, we lose the side of the order of information, because the side can be ordered results. Assuming that the original array is {1,2,3,3,3,3,3}, then the rotation may be {3,3,3,3,3,1,2}, or {3,1,2,3,3,3,3}, so that we judge the left edge and center of the time are 3, if we are looking for 1 or 2, We don't know which half we should jump to. The solution can only be to move the edge one step, until the edge and the middle is not equal or meet, which leads to the possibility that there will not be half cut. So the worst case scenario (such as all an element, or only one element is different from the other, and he is in the last one) will appear each move one step, a total of n steps, the algorithm's time complexity into O (n).


AC Code:

<span style= "Font-family:microsoft yahei;font-size:12px;" >public Boolean search (int[] A, int target) {    if (A==null | | A.length==0)        return false;    int l = 0;    int r = a.length-1;    while (l<=r)    {        int m = (l+r)/2;        if (A[m]==target)            return true;        if (A[m]>a[l])        {            if (a[m]>target && a[l]<=target)            {                r = m-1;            }            else            {                L = m+1;            }        }        else if (A[m]<a[l])        {            if (a[m]<target && a[r]>=target)            {                L = m+1;            }            else            {                r = m-1;            }                        }        else        {            l++;        }    }    return false;} </span>


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

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