-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path56.merge-intervals.js
More file actions
41 lines (40 loc) · 949 Bytes
/
56.merge-intervals.js
File metadata and controls
41 lines (40 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* @lc app=leetcode.cn id=56 lang=javascript
*
* [56] Merge Intervals
*/
// @lc code=start
/**
* @param {number[][]} intervals
* @return {number[][]}
*/
// [1,3],[2,6]
// [1,4],[4,5]
// s1 s2 s1[1] >= s2[0]
// current next
var merge = function(intervals) {
// 创建数组返回
let ans = [];
let len = intervals.length;
let index = 0;
let current = [];
intervals.sort((prev, next) => prev[0] - next[0])
while (index < len) {
let next = intervals[index++];
if (current.length === 0) {
current = next
} else {
if (current[1] >= next[0]) { // 能合并
current = [current[0], Math.max(current[1], next[1])];
} else { // 不能合并
ans.push([...current]);
current = next;
}
}
}
if (current.length > 0) {
ans.push([...current]);
}
return ans;
};
// @lc code=end