回溯 - 在排序数组中查找元素的第一个和最后一个位置
javascript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
let nums = [5, 7, 7, 8, 8, 10];
let target = 6;
let first_index = nums.indexOf(target);
if (first_index !== -1) {
for (let i = first_index; i < nums.length; i++) {
if (target !== nums[i]) {
console.log([first_index, i - 1]);
}
}
}
else {
console.log([-1, -1]);
}typescript
let nums=[5,7,7,8,8,10]
let target=6
let first_index=nums.indexOf(target)
if(first_index!==-1){
for(let i=first_index;i<nums.length;i++){
if(target!==nums[i]){
console.log([first_index,i-1])
}
}
}else{
console.log([-1,-1])
}