Leetcode 32nd -- search in rotated sorted Array

Source: Internet
Author: User

Suppose a sorted array is rotated at some unknown to you beforehand.

(I. e .,0 1 2 4 5 6 7Might become4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return-1.

You may assume no duplicate exists in the array.

The meaning of the question is that the original order is arranged, and then a certain number of digits is shifted to the right, and the subscript after the given value is changed is found in the transformed array. If you scan it once, you can also use accept, which means that the meaning of the question is lost. The question is to let us use the bipartite method. It is implemented with the complexity of O (logn), rather than the complexity of N.

This person has a good explanation as follows (describe his explanation and code first, and I will fix it later ):

The following figure shows an ordered array after rotate. The diagonal edges of the Quadrilateral indicate the values in the array.

In this case, the array is divided into two parts, which are sequential (ascending.

After we calculate the mid, there are two possibilities, represented by mid1 and mid2, respectively.

1. If a [low] <A [Mid], it indicates that the mid falls into the range 1, that is, the position of mid1 in the figure. If the target value is less than a [mid1], search between low and mid1; otherwise, search between mid1 and high;

2. If a [low]> = A [Mid], the mid falls into the range 2, that is, the position of mid2 in the figure. Similarly, if the target is less than a [mid2], continue to search between low and mid2; otherwise, search between mid2 and high.

In this way, on average, we remove half of the data each time, and the time complexity is O (logn ).

The Code is as follows:

Private Static int search2 (INT [] A, int target) {int Lo = 0; int HI =. length-1; while (Lo <= Hi) {int mid = lo + (Hi-Lo)/2; If (target = A [Mid]) return mid; if (A [Mid]> A [lo]) {// first judge the mid and leftmost values if (target> = A [lo] & target <A [Mid]) {HI = mid-1 ;} else {Lo = Mid + 1 ;}} else {If (target <= A [HI] & target> A [Mid]) {Lo = Mid + 1 ;} else {HI = mid-1 ;}} return-1 ;}

The above is the Java code. He first judges the size of a [Mid] And the leftmost part. If the example is [3, 1] and the target to be searched is 1, the problem may occur. Because first, when determining the left side, the mid and l subscripts are both 0, and the value 3 is greater than the target, then the judgment a [Mid]> A [l] is not true, so I think we should find it on the right. However, although target <= A [HI] is valid, target> A [Mid] is not valid because a [Mid] is greater than 3. therefore, execute HI = mid-1, and then cleverly miss the correct answer 1. cause output-1.

Considering this case, if you first consider the size of a [Mid] And the rightmost side, you can pass.

Class solution {public: int search (int A [], int N, int target) {int L = 0, r = n-1; while (L <= r) {int mid = (L + r)/2; if (a [Mid] = target) return mid; if (a [Mid] <A [R]) // compare it with the one on the right {if (a [Mid] <target & A [R]> = target) L = Mid + 1; else r = mid-1;} else {if (a [Mid]> Target & A [l] <= target) r = mid-1; else l = Mid + 1 ;}} return-1 ;}};

If you do want to start from the left side, you can use the ratio to depeat the value of Mid + or-1 instead of directly comparing it with mid, because mid has already determined whether it is equal to the target. This can also avoid previous errors. The Code is as follows:

Int search (int A [], int N, int target) {int Lo = 0; int HI = n-1; while (Lo <= Hi) {int mid = lo + (Hi-Lo)/2; If (target = A [Mid]) return mid; if (a [Mid]> A [lo]) {If (target> = A [lo] & target <= A [Mid-1]) {// If mid-1 is smaller than or equal to HI = mid-1 ;} else {Lo = Mid + 1 ;}} else {If (target <= A [HI] & target> = A [Mid + 1]) {Lo = Mid + 1 ;}else {HI = mid-1 ;}} return-1 ;}

 

Leetcode 32nd -- search in rotated sorted Array

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.