-
Notifications
You must be signed in to change notification settings - Fork 23
/
56.go
61 lines (51 loc) · 994 Bytes
/
56.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package problem
import "sort"
/*
给出一个区间的集合,请合并所有重叠的区间。
示例 1:
输入: [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
输入: [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
*/
type MS [][]int
func (m MS) Len() int {
return len(m)
}
func (m MS) Less(i, j int) bool {
if m[i][0] > m[j][0] {
return false
}
return true
}
func (m MS) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
func merge(intervals [][]int) [][]int {
s := len(intervals)
ret := make([][]int, 0)
if s == 0 {
return ret
}
sort.Sort(MS(intervals))
tmp := intervals[0]
for j := 1; j < s; j++ {
if tmp[1] >= intervals[j][0] {
tmp[1] = max(tmp[1], intervals[j][1])
} else {
ret = append(ret, tmp)
tmp = intervals[j]
}
}
ret = append(ret, tmp)
return ret
}