`

LeetCode - Search for a Range

阅读更多

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

Solution1:

做两次二分就可以了,第一次二分找出最左边的边界,第二次二分找出最右边的边界,这样,无论平均还是最差都是O(lgn)。

public int[] searchRange(int[] A, int target) {
    int[] range = {-1, -1};
    if(A == null || A.length == 0) return range;
    int l = 0;
    int r = A.length-1;
    // Search for lower bound
    while(l<=r) {
        int m = (l+r)/2;
        if(A[m]<target) {
            l=m+1;
        } else {
            r=m-1;
        }
    }
    // If the target is not found, return (-1, -1)
    if(l>=A.length || A[l] != target) return range;
    range[0] = l;
    
    // Search for upper bound
    r = A.length-1;
    while(l<=r) { // A[l ~ r] >= target
        int m = (l+r)/2; // A[m] >= target
        if(A[m]==target) {
            l=m+1;
        } else {
            r=m-1;
        }
    }
    range[1] = r;
    return range;
}

 

Solution2:

We can make target -0.5 for searching low bound and target+0. 5 for the high bound.

public int[] searchRange(int[] A, int target) {  
    if (A==null) return null;
    int[] range = {-1,-1};
    
    // Be care for there , low>=A.length must be check first or 
    // there may be a out of boundary exception cause 
    // the binarySearch function in this question return low instead of null
    // if the target are not in the array
    int low = binarySearch(A,target-0.5);
    if (low >= A.length || A[low]!=target){
        return range;
    }
    
    range[0] = low;
    range[1] = binarySearch(A,target+0.5)-1;
    return range;
}

public int binarySearch(int[] A, double t){
    int low = 0, high = A.length - 1;
    while(low <= high){
        int m = (low + high) / 2;
        if(A[m] == t) return m;
        if(A[m] > t) high = m - 1;
        else low = m + 1;
    }
    return low;
}

 

Solution 3:

public int[] searchRange(int[] nums, int target) {
    int left = getLeftIndex(nums, target);  
    int right = getRightIndex(nums, target);  
    return new int[]{left, right};  
}  
  
private int getLeftIndex(int[] a, int t) {  
    int start = 0, end = a.length-1;  
    while(start <= end) {  
        int mid = (start + end) / 2;  
        if((mid == 0 || a[mid-1] < t) && a[mid] == t) {  
            return mid;  
        } else if(a[mid] < t) {  
            start = mid + 1;  
        } else {  
            end = mid - 1;  
        }  
    }  
    return -1;  
}   
  
private int getRightIndex(int[] a, int t) {  
    int start = 0, end = a.length-1;  
    while(start <= end) {  
        int mid = (start + end) / 2;  
        if((mid == a.length-1 || a[mid+1] > t) && a[mid] == t) {  
            return mid;  
        } else if(a[mid] > t) {  
            end = mid - 1;
        } else {  
            start = mid + 1;
        }  
    }  
    return -1;  
}

 

Reference:

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

http://rleetcode.blogspot.com/2014/02/search-for-range-java.html

http://www.geeksforgeeks.org/count-number-of-occurrences-in-a-sorted-array/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics