Skip to content

Commit af17954

Browse files
aQuaaQua
aQua
authored and
aQua
committed
118 accepted
1 parent 24caa06 commit af17954

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

Algorithms/0118.pascals-triangle/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
22

33
## 题目
4+
Given numRows, generate the first numRows of Pascal's triangle.
45

5-
6+
For example, given numRows = 5, Return
7+
```
8+
[
9+
[1],
10+
[1,1],
11+
[1,2,1],
12+
[1,3,3,1],
13+
[1,4,6,4,1]
14+
]
15+
```
616
## 解题思路
717

818

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
package Problem0118
22

3+
func generate(numRows int) [][]int {
4+
res := [][]int{}
5+
if numRows == 0 {
6+
return res
7+
}
8+
9+
res = append(res, []int{1})
10+
if numRows == 1 {
11+
return res
12+
}
13+
14+
for i := 1; i < numRows; i++ {
15+
res = append(res, genNext(res[i-1]))
16+
}
17+
18+
return res
19+
}
20+
21+
func genNext(p []int) []int {
22+
temp := make([]int, 1)
23+
temp = append(temp, p...)
24+
temp = append(temp, 0)
25+
26+
for i := len(temp) - 1; i > 0; i-- {
27+
temp[i] += temp[i-1]
28+
}
29+
30+
return temp[1:]
31+
}
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0118
22

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

77
"github.com/stretchr/testify/assert"
88
)
@@ -15,13 +15,13 @@ type question struct {
1515
// para 是参数
1616
// one 代表第一个参数
1717
type para struct {
18-
one string
18+
one int
1919
}
2020

2121
// ans 是答案
2222
// one 代表第一个答案
2323
type ans struct {
24-
one string
24+
one [][]int
2525
}
2626

2727
func Test_Problem0118(t *testing.T) {
@@ -30,17 +30,35 @@ func Test_Problem0118(t *testing.T) {
3030
qs := []question{
3131

3232
question{
33-
para{""},
34-
ans{""},
33+
para{0},
34+
ans{[][]int{}},
35+
},
36+
37+
question{
38+
para{1},
39+
ans{[][]int{
40+
[]int{1},
41+
}},
3542
},
36-
43+
44+
question{
45+
para{5},
46+
ans{[][]int{
47+
[]int{1},
48+
[]int{1, 1},
49+
[]int{1, 2, 1},
50+
[]int{1, 3, 3, 1},
51+
[]int{1, 4, 6, 4, 1},
52+
}},
53+
},
54+
3755
// 如需多个测试,可以复制上方元素。
3856
}
3957

4058
for _, q := range qs {
4159
a, p := q.ans, q.para
4260
fmt.Printf("~~%v~~\n", p)
4361

44-
ast.Equal(a.one, (p.one), "输入:%v", p)
62+
ast.Equal(a.one, generate(p.one), "输入:%v", p)
4563
}
4664
}

0 commit comments

Comments
 (0)