-
Notifications
You must be signed in to change notification settings - Fork 0
/
56.go
46 lines (40 loc) · 888 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
package leetcode
import (
"sort"
)
type Intervals []Interval
func (intervals Intervals) Len() int {
return len(intervals)
}
func (intervals Intervals) Swap(i, j int) {
intervals[i], intervals[j] = intervals[j], intervals[i]
}
func (intervals Intervals) Less(i, j int) bool {
if intervals[i].Start < intervals[j].Start {
return true
} else if intervals[i].Start == intervals[j].Start && intervals[i].End < intervals[i].End {
return true
} else {
return false
}
}
func merge(intervals []Interval) []Interval {
ret := []Interval{}
sz := len(intervals)
if sz == 0 {
return ret
}
sort.Sort(Intervals(intervals))
preEle := intervals[0]
for i := 1; i < sz; i++ {
ele := intervals[i]
if ele.Start > preEle.End {
ret = append(ret, preEle)
preEle = ele
} else if ele.End > preEle.End {
preEle.End = ele.End
}
}
ret = append(ret, preEle)
return ret
}