public static int lowerBound(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = (left + right) / 2; if (nums[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return left; } public static int upperBound(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = (left + right) / 2; if (nums[mid] <= target) { left = mid + 1; } else { right = mid - 1; } } return left; } 最后一个小于等于目标值 public static int lastLE(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = (right - left) / 2 + left; if (nums[mid] <= target) { left = mid + 1; } else { right = mid - 1; } } return right; }