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].

 

这道题是二分查找Search Insert Position的 变体,思路并不复杂,就是先用二分查找找到其中一个target,然后再往左右找到target的边缘。找边缘的方法跟二分查找仍然是一样的,只是切半的 条件变成相等,或者不等(往左边找则是小于,往右边找则是大于)。这样下来总共进行了三次二分查找,所以算法的时间复杂度仍是O(logn),空间复杂度 是O(1)。 代码如下: 

  1. public int[] searchRange(int[] A, int target) {  
  2.     int[] res = new int[2];  
  3.     res[0] = -1;  
  4.     res[1] = -1;  
  5.     if(A==null || A.length==0)  
  6.     {  
  7.         return res;  
  8.     }  
  9.     int l=0;  
  10.     int r=A.length-1;  
  11.     int m=(l+r)/2;  
  12.     while(l<=r)  
  13.     {  
  14.         m=(l+r)/2;  
  15.         if(A[m]==target)  
  16.         {  
  17.             res[0]=m;  
  18.             res[1]=m;  
  19.             break;  
  20.         }  
  21.         else if(A[m]>target)  
  22.         {  
  23.             r = m-1;  
  24.         }  
  25.         else  
  26.         {  
  27.             l = m+1;  
  28.         }  
  29.     }  
  30.     if(A[m]!=target)  
  31.         return res;  
  32.     int newL = m;  
  33.     int newR = A.length-1;  
  34.     while(newL<=newR)  
  35.     {  
  36.         int newM=(newL+newR)/2;  
  37.         if(A[newM]==target)  
  38.         {  
  39.             newL = newM+1;  
  40.         }  
  41.         else  
  42.         {  
  43.             newR = newM-1;  
  44.         }              
  45.     }  
  46.     res[1]=newR;  
  47.     newL = 0;  
  48.     newR = m;  
  49.     while(newL<=newR)  
  50.     {  
  51.         int newM=(newL+newR)/2;  
  52.         if(A[newM]==target)  
  53.         {  
  54.             newR = newM-1;  
  55.         }  
  56.         else  
  57.         {  
  58.             newL = newM+1;  
  59.         }              
  60.     }  
  61.     res[0]=newL;          
  62.       
  63.     return res;  
  64. }  

有 朋友在留言中提到这里可以只用两次二分查找就足够了,确实如此。 如果我们不寻找那个元素先,而是直接相等的时候也向一个方向继续夹逼,如果向右夹逼,最后就会停在右边界,而向左夹逼则会停在左边界,如此用停下来的两个 边界就可以知道结果了,只需要两次二分查找。代码如下: 

  1. public int[] searchRange(int[] A, int target) {  
  2.     int[] res = {-1,-1};  
  3.     if(A==null || A.length==0)  
  4.     {  
  5.         return res;  
  6.     }  
  7.     int ll = 0;  
  8.     int lr = A.length-1;  
  9.     while(ll<=lr)  
  10.     {  
  11.         int m = (ll+lr)/2;  
  12.         if(A[m]<target)  
  13.         {  
  14.             ll = m+1;  
  15.         }  
  16.         else  
  17.         {  
  18.             lr = m-1;  
  19.         }  
  20.     }  
  21.     int rl = 0;  
  22.     int rr = A.length-1;  
  23.     while(rl<=rr)  
  24.     {  
  25.         int m = (rl+rr)/2;  
  26.         if(A[m]<=target)  
  27.         {  
  28.             rl = m+1;  
  29.         }  
  30.         else  
  31.         {  
  32.             rr = m-1;  
  33.         }  
  34.     }  
  35.     if(ll<=rr)  
  36.     {  
  37.         res[0] = ll;  
  38.         res[1] = rr;  
  39.     }  
  40.     return res;  
  41. }  

实现中用到了在Search Insert Position中提到的方法,可以保证当搜索结束时,l和r所停的位置正好是目标数的后面和前面。二分查找的题目还是比较常考的,既带有一点算法思想,实现上也不会过于复杂,很适合作为面试题,类似的题目还有Search in Rotated Sorted Array

 

 

class Solution {
public:
    vector<int> searchRange(int A[], int n, int target) {
        vector<int> result({-1, -1});
        if (n == 0) return result;
        
        int i = 0, j = n - 1;
        while (i <= j) {
            int k = i + (j - i) / 2;
            if (A[k] < target) i = k + 1;
            else j = k - 1;
        }
        
        if (i > n - 1 || A[i] != target) return result;
        result[0] = i;
        
        j = n - 1;
        while (i <= j) {
            int k = i + (j - i) / 2;
            if (A[k] <= target) i = k + 1;
            else j = k - 1;
        }
        result[1] = j;
    
        return result;
    }
};

 

posted on 2015-01-10 22:01  风云逸  阅读(76)  评论(0)    收藏  举报