Skip to content

Commit 2c2ca37

Browse files
authored
Merge pull request #49 from imjma/problem-118
Add Problem 118
2 parents 7989003 + c108fb9 commit 2c2ca37

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
func generate(numRows int) [][]int {
4+
var result [][]int
5+
6+
for i := 0; i < numRows; i++ {
7+
var row []int
8+
9+
for j := 0; j < i+1; j++ {
10+
if j == 0 || j == i {
11+
row = append(row, 1)
12+
} else if i > 1 {
13+
row = append(row, result[i-1][j-1]+result[i-1][j])
14+
}
15+
}
16+
17+
result = append(result, row)
18+
}
19+
20+
return result
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question118 struct {
9+
para118
10+
ans118
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para118 struct {
16+
numRows int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans118 struct {
22+
one [][]int
23+
}
24+
25+
func Test_Problem118(t *testing.T) {
26+
27+
qs := []question118{
28+
29+
question118{
30+
para118{2},
31+
ans118{[][]int{{1}, {1, 1}}},
32+
},
33+
34+
question118{
35+
para118{5},
36+
ans118{[][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}}},
37+
},
38+
39+
question118{
40+
para118{10},
41+
ans118{[][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}, {1, 5, 10, 10, 5, 1}, {1, 6, 15, 20, 15, 6, 1}, {1, 7, 21, 35, 35, 21, 7, 1}, {1, 8, 28, 56, 70, 56, 28, 8, 1}, {1, 9, 36, 84, 126, 126, 84, 36, 9, 1}}},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 118------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans118, q.para118
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, generate(p.numRows))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/
2+
3+
4+
## 题目
5+
6+
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
7+
8+
![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
9+
10+
**Note:** In Pascal's triangle, each number is the sum of the two numbers directly above it.
11+
12+
**Example:**
13+
14+
```
15+
Input: 5
16+
Output:
17+
[
18+
[1],
19+
[1,1],
20+
[1,2,1],
21+
[1,3,3,1],
22+
[1,4,6,4,1]
23+
]
24+
```
25+
26+
## 题目大意
27+
28+
给一个正整数来生成一个帕斯卡三角形
29+
30+
## 解题思路
31+
32+

0 commit comments

Comments
 (0)