Search in rotated Sorted Array Ii-leetcode

Source: Internet
Author: User

It is precisely because of this condition, there is a more complex case, even affect the algorithm's time complexity. 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).

Like the last one, it's just a little bit more complicated.

BOOL Rotatedarray_second (vector<int>& vec,int key) {int low =0,high= vec.size () -1;int mid;while (Low <= high ) {cout<<low<< "" <



Search in rotated Sorted Array Ii-leetcode

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.