Skip to content

Commit c3a97c9

Browse files
aQuaaQua
aQua
authored and
aQua
committed
119 accepted & finish
1 parent d847680 commit c3a97c9

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# [119. Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)
2+
3+
## 题目
4+
Given an index k, return the kth row of the Pascal's triangle.
5+
6+
For example, given k = 3,
7+
Return [1,3,3,1].
8+
9+
Note:
10+
Could you optimize your algorithm to use only O(k) extra space?
11+
12+
## 解题思路
13+
14+
见程序注释
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Problem0119
2+
3+
func getRow(rowIndex int) []int {
4+
res := make([]int, 1, rowIndex+1)
5+
res[0] = 1
6+
if rowIndex == 0 {
7+
return res
8+
}
9+
10+
for i := 0; i < rowIndex; i++ {
11+
res = append(res, 1)
12+
for j := len(res) - 2; j > 0; j-- {
13+
res[j] += res[j-1]
14+
}
15+
}
16+
17+
return res
18+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package Problem0119
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type question struct {
11+
para
12+
ans
13+
}
14+
15+
// para 是参数
16+
type para struct {
17+
rowIndex int
18+
}
19+
20+
// ans 是答案
21+
type ans struct {
22+
one []int
23+
}
24+
25+
func Test_Problem0119(t *testing.T) {
26+
ast := assert.New(t)
27+
28+
qs := []question{
29+
30+
question{
31+
para{
32+
3,
33+
},
34+
ans{
35+
[]int{1, 3, 3, 1},
36+
},
37+
},
38+
39+
question{
40+
para{
41+
0,
42+
},
43+
ans{
44+
[]int{1},
45+
},
46+
},
47+
// 如需多个测试,可以复制上方元素。
48+
}
49+
50+
for _, q := range qs {
51+
a, p := q.ans, q.para
52+
fmt.Printf("~~%v~~\n", p)
53+
54+
ast.Equal(a.one, getRow(p.rowIndex), "输入:%v", p)
55+
}
56+
}

0 commit comments

Comments
 (0)