"034-search for a range (search for a scope)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
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]
.
Main Topic
Given a sorted array, given a value to look up, find out where the number appears in the array at the start and end positions.
The time complexity of the algorithm is called log (N).
If not found, return [-1, -1]
Thinking of solving problems
(assuming that the array is incrementally ordered) the binary lookup algorithm is used to see if the number exists in the array and returns [-1,-1]
if it does not exist.
If there is one, find the last position of the number and the first occurrence. Find the last position, first from the array to find the last position, if it is greater than the value to be found, near a position, also greater than the previous nearly 2, to 2 to find the number of exponential increase, if less than to return to the original position after a search, re-press 1,2,4 、、、 this way, until found. The same is true for finding the first element.
Code Implementation
Algorithm implementation class
Public class solution { Public int[]Searchrange(int[] A,intTarget) {if(A = =NULL|| A.length = =0) {return New int[]{-1, -1}; }intLo =0;inthi = A.length-1;intMi =0;//Find out if an element with a value of target exists in the array while(Lo <= hi) {mi = lo + (hi-lo)/2;if(Target < A[mi]) {hi = Mi-1; }Else if(Target > A[mi]) {lo = mi +1; }Else{ Break; } }if(A[mi]! = target) {return New int[]{-1, -1}; } lo = Searchfirst (A,0, MI, target); hi = Searchlast (A, MI, a.length-1, target);return New int[]{lo, HI}; }/** * Find the position where target first appears, look for the range [lo, Hi],a[hi] equals target,a to an ordered array * * @param A to find the array * @para the starting position of the M Lo Lookup * @param The end position of the Hi lookup * @param The value of Target lookup * @return Target first occurrence position * / Private int Searchfirst(int[] A,intLointFibintTarget) {intGap =1; do {hi-= gap;if(Hi < Lo | | A[HI]! = target) {hi + = gap;if(Hi <= Lo | | A[hi-1] = target) {returnHi }Else{gap =1; hi--; } }Else{//In the last position back gap is still equalGap *=2; } } while(true); }/** * Find the last position of target, look for the range of [Lo, Hi],a[lo] equals target,a as an ordered array * * @param A to find the array * @para the starting position of the M Lo Lookup * @param The end position of the Hi lookup * @param The value of Target lookup * @return Target Last place to appear * / Private int Searchlast(int[] A,intLointFibintTarget) {intGap =1; do {lo + = gap;if(Lo > Hi | | A[lo]! = target) {lo-= gap;if(Lo >= Hi | | A[lo +1] = target) {returnLo }Else{gap =1; lo++; } }Else{//In the last position the gap is still equalGap *=2; } } while(true); }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47079319"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "034-search for a range (search for a scope)"