Search for a Range leetcode java


題目:

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

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

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

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

 

從題目中獲取到關鍵字:sorted array ,find...position, a given target value, O(logn).

這些關鍵字都在提醒我們這道題的思路應該是二分查找法


       二分查找方法
       二分查找經常用來在有序的數列查找某個特定的位置。

       因此,應用二分查找法,這個數列必須包含以下特征:

  •  存儲在數組中
  • 有序排列

     同時這道題讓我們確定 starting position和ending position,這就讓我們聯想到之前做過的Search Insert Position一題,當無法查找到given target時,利用非遞歸二分查找法所得的最終low和high指針,將會指向該無法查找到的元素的左右兩個元素。說不清楚看例子,例如,給定arraylist [1,2,4,5] target為3,那么通過傳統非遞歸二分查找法,low指針將會指向4(位置為2),high指針指向2(位置為1)。

     利用這種規律,我們就能夠找到target元素的左右邊界。所以本題的解題思路為:

第一步,在給定數組中找到該target,記錄該位置。這時我們並不關心這個target是邊界還是中間值,我們只需確定,在數組中是能夠找到這樣一個target值。如果找不到返回{-1,-1}。為了保證時間復雜度是O(logn), 這里自然而然使用傳統二分查找法實現。

第二步,確定該target的右邊界。此時我們將對數組從剛才確定的那個target的pos作為起始點,到數組結束,來確定右邊界。同樣是使用二分查找法,當新的mid值仍然等於target值時,我們能確定該mid左半邊(到pos)都是等於target,繼續在右半邊查找。如果新的mid值不等於target值,我們就知道右邊界一定在新mid值的左半邊,繼續查找。最后新的high指針指向的就是右邊界的位置。

第三步,確定該target的左邊界。這一步與第二步對稱操作,最后新的low指針指向的就是左邊界的位置。

最后,返回結果數組。

 

代碼如下:

 

 1      public  int[] searchRange( int[] A,  int target) {
 2          int [] res = {-1,-1};
 3          if(A ==  null || A.length == 0)
 4              return res;
 5         
 6          // first iteration, find target wherever it is
 7           int low = 0;
 8          int high = A.length-1;
 9          int pos = 0;
10          while(low <= high){
11              int mid = (low + high)/2;
12             pos = mid;
13              if(A[mid] > target)
14                 high = mid - 1;
15              else  if(A[mid] < target)
16                 low = mid + 1;
17              else{
18                 res[0] = pos;
19                 res[1] = pos;
20                  break;
21             }
22         }
23         
24          if(A[pos] != target)
25              return res;
26         
27          // second iteration, find the right boundary of this target
28           int newlow = pos;
29          int newhigh = A.length-1;
30          while(newlow <= newhigh){
31              int newmid = (newlow+newhigh)/2;
32              if(A[newmid] == target)
33                 newlow = newmid + 1;
34              else
35                 newhigh = newmid - 1;
36         }
37         res[1] = newhigh;
38         
39          // third iteration, find the left boundary of this target
40          newlow = 0;
41         newhigh = pos;
42          while(newlow <= newhigh){
43              int newmid = (newlow+newhigh)/2;
44              if(A[newmid] == target)
45                 newhigh = newmid - 1;
46              else
47                 newlow = newmid + 1;
48         }
49         res[0] = newlow;
50         
51          return res;
52     }

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM