Skip to content

Commit 65ff263

Browse files
aQuaaQua
aQua
authored and
aQua
committed
56 没有深入理解题意,准备大改程序结构
1 parent d898fde commit 65ff263

File tree

3 files changed

+131
-9
lines changed

3 files changed

+131
-9
lines changed

Algorithms/0056.merge-intervals/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
## 题目
44
Given a collection of intervals, merge all overlapping intervals.
55

6-
6+
```
77
For example,
88
Given [1,3],[2,6],[8,10],[15,18],
99
return [1,6],[8,10],[15,18].
10+
```
1011
## 解题思路
1112

1213
见程序注释
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
package Problem0056
22

3+
// Interval Definition for an interval.
4+
type Interval struct {
5+
Start int
6+
End int
7+
}
8+
39
func merge(intervals []Interval) []Interval {
10+
if len(intervals) <= 1 {
11+
return intervals
12+
}
13+
14+
res := make([]Interval, 0, len(intervals))
15+
temp := intervals[0]
16+
17+
for i := 1; i < len(intervals); i++ {
18+
if isOverlap(temp, intervals[i]) {
19+
temp = mergeInterval(temp, intervals[i])
20+
continue
21+
}
22+
res = append(res, temp)
23+
temp = intervals[i]
24+
}
25+
26+
res = append(res, temp)
27+
28+
return res
29+
}
30+
31+
func isOverlap(a, b Interval) bool {
32+
return (a.Start <= b.Start && b.Start <= a.End) || (a.Start <= b.End && b.End <= a.End) || (b.Start <= a.Start && a.Start <= b.End)
33+
}
34+
35+
func mergeInterval(a, b Interval) Interval {
36+
return Interval{
37+
Start: min(a.Start, b.Start),
38+
End: max(a.End, b.End),
39+
}
40+
}
41+
42+
func min(a, b int) int {
43+
if a < b {
44+
return a
45+
}
46+
return b
47+
}
448

49+
func max(a, b int) int {
50+
if a > b {
51+
return a
52+
}
53+
return b
554
}

Algorithms/0056.merge-intervals/merge-intervals_test.go

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0056
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -19,7 +19,7 @@ type para struct {
1919

2020
// ans 是答案
2121
type ans struct {
22-
one []Interval
22+
one []Interval
2323
}
2424

2525
func Test_Problem0056(t *testing.T) {
@@ -28,21 +28,93 @@ func Test_Problem0056(t *testing.T) {
2828
qs := []question{
2929

3030
question{
31-
para{
32-
,
31+
para{[]Interval{
32+
Interval{2, 3},
33+
Interval{4, 5},
34+
Interval{6, 7},
35+
Interval{8, 9},
36+
Interval{1, 10},
37+
},
38+
},
39+
ans{[]Interval{
40+
Interval{1, 10},
41+
},
42+
},
43+
},
44+
45+
question{
46+
para{[]Interval{
47+
Interval{1, 4},
48+
Interval{0, 5},
49+
},
50+
},
51+
ans{[]Interval{
52+
Interval{0, 5},
53+
},
54+
},
55+
},
56+
57+
question{
58+
para{[]Interval{
59+
Interval{1, 4},
60+
Interval{2, 5},
61+
},
62+
},
63+
ans{[]Interval{
64+
Interval{1, 5},
3365
},
34-
ans{
35-
,
3666
},
3767
},
38-
68+
69+
question{
70+
para{[]Interval{
71+
Interval{1, 3},
72+
Interval{2, 6},
73+
Interval{8, 10},
74+
Interval{15, 18},
75+
}},
76+
ans{[]Interval{
77+
Interval{1, 6},
78+
Interval{8, 10},
79+
Interval{15, 18},
80+
},
81+
},
82+
},
83+
84+
question{
85+
para{[]Interval{
86+
Interval{1, 6},
87+
},
88+
},
89+
ans{[]Interval{
90+
Interval{1, 6},
91+
},
92+
},
93+
},
94+
3995
// 如需多个测试,可以复制上方元素。
4096
}
4197

4298
for _, q := range qs {
4399
a, p := q.ans, q.para
44100
fmt.Printf("~~%v~~\n", p)
45101

46-
ast.Equal(a.one, merge(p. ), "输入:%v", p)
102+
ast.Equal(a.one, merge(p.intervals), "输入:%v", p)
47103
}
48104
}
105+
106+
func Test_min(t *testing.T) {
107+
ast := assert.New(t)
108+
109+
actual := min(1, 0)
110+
expected := 0
111+
ast.Equal(expected, actual)
112+
}
113+
114+
func Test_max(t *testing.T) {
115+
ast := assert.New(t)
116+
117+
actual := max(1, 0)
118+
expected := 1
119+
ast.Equal(expected, actual)
120+
}

0 commit comments

Comments
 (0)