解决思路
二分查找
//left表示是否为寻找左边坐标
private int extremeInsertionIndex(int[] nums,int target,boolean left){
int l = 0;
int r = nums.length;
//二分法
while (l < r){
int mid = (l + r) / 2;
if (nums[mid] > target || (left && target == nums[mid]))
r = mid;
else
l = mid + 1;
}
return l;
}
public int[] searchRange(int[] nums,int target){
int[] targetRange = {-1,-1};
//先寻找左边的坐标
int leftIdx = extremeInsertionIndex(nums,target,true);
if (leftIdx == nums.length || nums[leftIdx] != target)
return targetRange;
targetRange[0] = leftIdx;
//再寻找有右边的坐标
targetRange[1] = extremeInsertionIndex(nums,target,false)-1;
return targetRange;
}