碎碎念,每次自己都钻牛角尖,相出来的解法打了n个补丁也过不了,有更好的思路但是想不到,还是换个思路更好,卑微看答案了,遍历一次,统计区间
/**
* @param {number[][]} intervals
* @param {number[]} newInterval
* @return {number[][]}
*/
var insert = function(intervals, newInterval) {
const res = []
let i = 0;
const len = intervals.length
while(i<len && intervals[i][1]<newInterval[0]){
res.push(intervals[i])
i++
}
while(i<len && intervals[i][0]<=newInterval[1]){
newInterval[0] = Math.min(newInterval[0], intervals[i][0])
newInterval[1] = Math.max(newInterval[1], intervals[i][1])
i++
}
res.push(newInterval)
while(i<len){
res.push(intervals[i])
i++
}
return res
}