-
Notifications
You must be signed in to change notification settings - Fork 0
/
review300.go
45 lines (37 loc) · 864 Bytes
/
review300.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
package main
import "sort"
type SummaryRanges struct {
mp map[int]bool
}
func Constructor() SummaryRanges {
return SummaryRanges{ map[int]bool{}}
}
func (this *SummaryRanges) AddNum(value int) {
if !this.mp[value] {
this.mp[value] = true
}
}
func (this *SummaryRanges) GetIntervals() [][]int {
values := []int{}
for v := range this.mp {
values = append(values, v)
}
sort.Slice(values, func(a, b int) bool {
return values[a] < values[b]
})
result := [][]int{}
for i := range values {
if len(result) > 0 && result[len(result)-1][1] + 1 == values[i] {
result[len(result)-1][1] = values[i]
}else {
result = append(result, []int{values[i], values[i]})
}
}
return result
}
/**
* Your SummaryRanges object will be instantiated and called as such:
* obj := Constructor();
* obj.AddNum(value);
* param_2 := obj.GetIntervals();
*/