先排序,再循环比较右值与下个区间左值大小,如果大于再比较右值与下个区间右值,将符合条件区间压进目标数组,继续循环至结束
* function Interval(a, b){
* this.start = a || 0;
* this.end = b || 0;
* }
*/
/**
*
* @param intervals Interval类一维数组
* @return Interval类一维数组
*/
function merge( intervals ) {
// write code here
let len = intervals.length;
let arr = [];
let j = 0;
intervals.sort(function(a,b){return a.start - b.start});
while(j < len){
while(j < len - 1 && intervals[j].end >= intervals[j + 1].start){
j++;
intervals[j].start = intervals[j - 1].start;
if(intervals[j].end < intervals[j - 1].end){
intervals[j].end = intervals[j - 1].end;
}
}
arr.push(intervals[j]);
j++;
}
return arr;
}
module.exports = {
merge : merge
};