-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path合并区间.js
More file actions
31 lines (31 loc) · 707 Bytes
/
合并区间.js
File metadata and controls
31 lines (31 loc) · 707 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
// s1 s2 s1[1] >= s2[0]
// current next
/**
* @param {number[][]} intervals
* @return {number[][]}
56. 合并区间
*/
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) {
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
};