*search for a Range

Source: Internet
Author: User

Topic:

Given a sorted array of integers, find the starting and ending position of a Given target value.

Your algorithm ' s runtime complexity must is in the order of O(log n).

If the target is not a found in the array, return [-1, -1] .

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
Return [3, 4] .

Get the keyword from the title: sorted array, find...position, a given target value, O (LOGN).

These keywords are reminding us that the idea of this problem should be a binary search method .

Two-Part search method
Binary lookups are often used to find a particular location in an ordered sequence of numbers.

Therefore, using the binary lookup method, this sequence must contain the following characteristics:

    • stored in the array
    • Orderly arrangement

At the same time this problem let us determine starting position and ending position, which reminds us of the previous search Insert position, when unable to find given target, The final low and high pointers obtained using the non-recursive binary lookup method will point to the left and right two elements of the element that cannot be found. For example, given a ArrayList [1,2,4,5] target of 3, the low pointer will point to 4 (position 2) with the traditional non-recursive binary lookup method, and the high pointer to 2 (position 1).

Using this rule, we can find the right and left borders of the target element. So the solution to the problem is:

The first step is to find the target in the given array and record the location. At this point we don't care whether the target is a boundary or an intermediate value, we just need to be sure that the target value can be found in the array. returns { -1,-1} if not found. To ensure that the time complexity is O (LOGN), it is natural to use the traditional binary search method.

The second step is to determine the right boundary of the target. At this point we will determine the right boundary from the POS of the target that we just identified as the starting point, to the end of the array. Also using the binary lookup method, when the new mid value is still equal to the target value, we can determine that the mid left half (to POS) is equal to target and continues to look in the right half. If the new mid value is not equal to the target value, we know that the right boundary must be on the left half of the new mid value and continue looking. The last new high pointer points to the position of the right edge.

The third step is to determine the left edge of the target. This step is symmetric with the second step, and the last new low pointer points to the position of the left edge.

Finally, the result array is returned.

Code

1  Public int[] Searchrange (int[] A,inttarget) {2         int[] res = { -1,-1};3         if(A = =NULL|| A.length = = 0)4             returnRes;5         6         //First iteration, find target wherever it is7         intLow = 0;8         intHigh = A.length-1;9         intpos = 0;Ten          while(Low <=High ) { One             intMid = (low + high)/2; Apos =mid; -             if(A[mid] >target) -High = Mid-1; the             Else if(A[mid] <target) -Low = mid + 1; -             Else{ -Res[0] =Pos; +RES[1] =Pos; -                  Break; +             } A         } at          -         if(A[pos]! =target) -             returnRes; -          -         //second iteration, find the right boundary for this target -         intNewlow =Pos; in         intNewhigh = a.length-1; -          while(Newlow <=Newhigh) { to             intNewmid = (Newlow+newhigh)/2; +             if(A[newmid] = =target) -Newlow = Newmid + 1; the             Else *Newhigh = newmid-1; $         }Panax NotoginsengRES[1] =Newhigh; -          the         //third iteration, find the left boundary of this target +Newlow = 0; ANewhigh =Pos; the          while(Newlow <=Newhigh) { +             intNewmid = (Newlow+newhigh)/2; -             if(A[newmid] = =target) $Newhigh = newmid-1; $             Else -Newlow = Newmid + 1; -         } theRes[0] =Newlow; -         Wuyi         returnRes; the}

Reference:

http://blog.csdn.net/linhuanmars/article/details/20593391

Http://www.cnblogs.com/springfor/p/3857704.html

*search for a Range

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.