题解 | #合并区间#
合并区间
https://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a
/* * function Interval(a, b){ * this.start = a || 0; * this.end = b || 0; * } */ /** * * @param intervals Interval类一维数组 * @return Interval类一维数组 */ function merge(intervals) { let result = []; if(intervals.length>=1) { intervals.sort((a,b) => {return a.start-b.start;}) result.push(intervals[0]); for( let i=1; i<intervals.length; i++) { const len = result.length; if(result[len-1].end>=intervals[i].start) { if(result[len-1].end<intervals[i].end) { result[len-1].end = intervals[i].end; } } else { result.push(intervals[i]); } } } return result; } module.exports = { merge: merge, };