二分查找

public int find(int[] arr, int target) {
    int left = 0, right = arr.length;
    while(left < right) {
        int mid = left + (right - left) / 2;
        if(arr[mid] < target) {
            left = mid + 1;
        }
        else {
            right = mid;
        }
    }
    /*查找第一个不小于target的数*/
    return right == arr.length ? -1 : arr[right];
    /*查找最后一个小于target的数*/
    //return right == arr.length ? -1 : arr[right - 1];
}
public int find(int[] arr, int target) {
    int left = 0, right = arr.length;
    while(left < right) {
        int mid = left + (right - left) / 2;
        if(arr[mid] <= target) {
            left = mid + 1;
        }
        else {
            right = mid;
        }
    }
    /*查找第一个大于target的数*/
    return right == arr.length ? -1 : arr[right];
    /*查找最后一个不大于target的数*/
    //return right == arr.length ? -1 : arr[right - 1];
}
powered by Gitbook最后修订时间: 2020-05-26 08:16:36

results matching ""

    No results matching ""