The sword refers to Offer-53 to find the number I in the sorted array

public int search(int[] nums, int target) {
    // 找target的右边界 - target-1的右边界
    return helper(nums, target) - helper(nums, target - 1);
}
int helper(int[] nums, int tar) {
    int i = 0;
    int j = nums.length - 1;
    // 二分法查找
    while(i <= j) {
        int m = (i + j) / 2;
        if(nums[m] <= tar) {
            i = m + 1;
        } else {
            j = m - 1;
        }
    }
    return i;
}

Guess you like

Origin blog.csdn.net/a792396951/article/details/114140484