题目
题解
这道题的关键是如何使用二分算法将符条件的最左面的值和最右面的值找到,不可以投机取巧,因为你不知道你要找的值会在数组中重复多少次出现
如何找最左面符合条件的值。
平常我使用二分算法都是找中间值,但是却没想到 >= 和<=情况下指针的指向,当找到一个符合条件的值后它的附近说不定还有重复值,利用大于等于和小于大于让指针继续往下走,这时候你就的想想指针怎吗停止判断,实在想不出来就画图。找到最右面的值也是这个思路
代码
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
* 一个升序的数组
* [1,2,3,3,3,3,4,5,9]
* [3,3,3]
* [1,4]
* [1]
* 找到最左边符合条件的数字
* l <= r 时 进行循环
* m >= target r = m - 1
* m < target l = m +1
* 结束时左指针指向最左边的符合条件的数字
* 找到最右边符合条件的数字
* l <= r
* m > target r = m - 1
* l <= target l = m + 1
*/
var searchRange = function(nums, target) {
if(nums.length < 1) return [-1,-1];
const l = searchLeftRange(nums, target);
const r = searchRightRange(nums, target);
const len = nums.length;
if(l > -1 && r > -1) {
return [l ,r]
}else if (l > -1){
return [l, l]
}else if (r > -1) {
return [r, r]
}else {
return [-1, -1]
}
};
function searchLeftRange (nums, target) {
let l = 0;
let r = nums.length - 1;
let mid = 0;
while(l <= r) {
mid = (l + r) >> 1
if(nums[mid] >= target) r = mid -1
else l = mid + 1
}
return nums[l] == target ? l : -1;
}
function searchRightRange(nums, target) {
let l = 0;
let r = nums.length - 1;
let mid = 0;
while(l <= r) {
mid = (l + r) >> 1
if(nums[mid] > target) r = mid - 1
else l = mid + 1
}
return nums[r] == target ? r : -1;
}
优化后
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
* 一个升序的数组
* [1,2,3,3,3,3,4,5,9]
* [3,3,3]
* [1,4]
* [1]
* 找到最左边符合条件的数字
* l < r 时 进行循环
* m >= target r = m - 1
* m < target l = m +1
* 结束时左指针指向最左边的符合条件的数字
* 找到最右边符合条件的数字
* l <= r
* m > target r = m - 1
* l <= target l = m + 1
*/
var searchRange = function(nums, target) {
if(nums.length < 1) return [-1,-1];
const l = searchLeftRange(nums, target);
const r = searchRightRange(nums, target);
return [l, r]
};
function searchLeftRange (nums, target) {
let l = 0;
let r = nums.length - 1;
let mid = 0;
while(l <= r) {
mid = (l + r) >> 1
if(nums[mid] >= target) r = mid -1
else l = mid + 1
}
return nums[l] == target ? l : -1;
}
function searchRightRange(nums, target) {
let l = 0;
let r = nums.length - 1;
let mid = 0;
while(l <= r) {
mid = (l + r) >> 1
if(nums[mid] > target) r = mid - 1
else l = mid + 1
}
return nums[r] == target ? r : -1;
}
将searchLeftRange函数与searchRightRange函数整合到searchRange函数表达式中
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
* 一个升序的数组
* [1,2,3,3,3,3,4,5,9]
* [3,3,3]
* [1,4]
* [1]
* 找到最左边符合条件的数字
* l < r 时 进行循环
* m >= target r = m - 1
* m < target l = m +1
* 结束时左指针指向最左边的符合条件的数字
* 找到最右边符合条件的数字
* l <= r
* m > target r = m - 1
* l <= target l = m + 1
*/
var searchRange = function(nums, target) {
if(nums.length < 1) return [-1,-1];
const leftRange = () => {
let l = 0;
let r = nums.length - 1;
let mid = 0;
while(l <= r) {
mid = (l + r) >> 1;
if(nums[mid] >= target) r = mid -1;
else l = mid + 1;
}
return nums[l] == target ? l : -1;
};
const reightRange = () => {
let l = 0;
let r = nums.length - 1;
let mid = 0;
while(l <= r) {
mid = mid = (l + r) >> 1;
if(nums[mid] > target) r = mid -1;
else l = mid + 1;
}
return nums[r] == target ? r : -1;
};
return [leftRange(), reightRange()]
};