输入: nums = [5,7,7,8,8,10], target = 8
输出: [3,4]
输入: nums = [5,7,7,8,8,10], target = 6
输出: [-1,-1]
class Solution {
public int[] searchRange(int[] nums, int target) {
int[] targetRange = {-1, -1};
int leftIndex = getIndex(nums, target, true);
// 如果越界了或者值对不上,就返回 -1 -1
if (leftIndex == nums.length || nums[leftIndex] != target) {
return targetRange;
}
//成功找到了左边界
targetRange[0] = leftIndex;
// 有左一定有右
// -1 是因为 在 getIndex() 中 right 为 nums.length
targetRange[1] = getIndex(nums, target, false) - 1;
return targetRange;
}
/**
* 返回左右索引值
* @param nums 数组
* @param target 目标值
* @param flag flag为true时递归查询左区间,flag为false时递归查询右区间
* @return
*/
public int getIndex(int[] nums, int target, boolean flag) {
int left = 0;
int right = nums.length;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target || (flag && nums[mid] == target)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
class Solution {
fun searchRange(nums: IntArray, target: Int): IntArray {
val targetRange = intArrayOf(-1, -1)
val leftIndex = getIndex(nums, target, true)
// 如果越界了或者值对不上,就返回 -1 -1
if (leftIndex == nums.size || nums[leftIndex] != target) {
return targetRange
}
//成功找到了左边界
targetRange[0] = leftIndex
// 有左一定有右
// -1 是因为 在 getIndex() 中 right 为 nums.size
targetRange[1] = getIndex(nums, target, false) - 1
return targetRange
}
/**
* 返回左右索引值
* @param nums 数组
* @param target 目标值
* @param flag flag为true时递归查询左区间,flag为false时递归查询右区间
* @return
*/
fun getIndex(nums: IntArray, target: Int, flag: Boolean): Int {
var left = 0
var right = nums.size
while (left < right) {
val mid = left + (right - left) / 2
if (nums[mid] > target || flag && nums[mid] == target) {
right = mid
} else {
left = mid + 1
}
}
return left
}
}