Skip to content

Commit c830ce2

Browse files
committed
Cookbook add 0118 solution
1 parent 2c2ca37 commit c830ce2

File tree

5 files changed

+93
-8
lines changed

5 files changed

+93
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package leetcode
22

33
func generate(numRows int) [][]int {
4-
var result [][]int
5-
4+
result := [][]int{}
65
for i := 0; i < numRows; i++ {
7-
var row []int
8-
6+
row := []int{}
97
for j := 0; j < i+1; j++ {
108
if j == 0 || j == i {
119
row = append(row, 1)
1210
} else if i > 1 {
1311
row = append(row, result[i-1][j-1]+result[i-1][j])
1412
}
1513
}
16-
1714
result = append(result, row)
1815
}
19-
2016
return result
2117
}

leetcode/0118.Pascals-Triangle/README.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/
1+
# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
22

33

44
## 题目
@@ -25,8 +25,36 @@ Output:
2525

2626
## 题目大意
2727

28-
给一个正整数来生成一个帕斯卡三角形
28+
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。在杨辉三角中,每个数是它左上方和右上方的数的和。
29+
2930

3031
## 解题思路
3132

33+
- 给定一个 n,要求打印杨辉三角的前 n 行。
34+
- 简单题。按照杨辉三角的生成规则循环打印即可。
35+
36+
37+
## 代码
38+
39+
```go
40+
41+
package leetcode
42+
43+
func generate(numRows int) [][]int {
44+
result := [][]int{}
45+
for i := 0; i < numRows; i++ {
46+
row := []int{}
47+
for j := 0; j < i+1; j++ {
48+
if j == 0 || j == i {
49+
row = append(row, 1)
50+
} else if i > 1 {
51+
row = append(row, result[i-1][j-1]+result[i-1][j])
52+
}
53+
}
54+
result = append(result, row)
55+
}
56+
return result
57+
}
58+
59+
```
3260

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。在杨辉三角中,每个数是它左上方和右上方的数的和。
29+
30+
31+
## 解题思路
32+
33+
- 给定一个 n,要求打印杨辉三角的前 n 行。
34+
- 简单题。按照杨辉三角的生成规则循环打印即可。
35+
36+
37+
## 代码
38+
39+
```go
40+
41+
package leetcode
42+
43+
func generate(numRows int) [][]int {
44+
result := [][]int{}
45+
for i := 0; i < numRows; i++ {
46+
row := []int{}
47+
for j := 0; j < i+1; j++ {
48+
if j == 0 || j == i {
49+
row = append(row, 1)
50+
} else if i > 1 {
51+
row = append(row, result[i-1][j-1]+result[i-1][j])
52+
}
53+
}
54+
result = append(result, row)
55+
}
56+
return result
57+
}
58+
59+
```
60+

website/content/menu/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ headless: true
125125
- [0112.Path-Sum]({{< relref "/ChapterFour/0112.Path-Sum.md" >}})
126126
- [0113.Path-Sum-II]({{< relref "/ChapterFour/0113.Path-Sum-II.md" >}})
127127
- [0114.Flatten-Binary-Tree-to-Linked-List]({{< relref "/ChapterFour/0114.Flatten-Binary-Tree-to-Linked-List.md" >}})
128+
- [0118.Pascals-Triangle]({{< relref "/ChapterFour/0118.Pascals-Triangle.md" >}})
128129
- [0120.Triangle]({{< relref "/ChapterFour/0120.Triangle.md" >}})
129130
- [0121.Best-Time-to-Buy-and-Sell-Stock]({{< relref "/ChapterFour/0121.Best-Time-to-Buy-and-Sell-Stock.md" >}})
130131
- [0122.Best-Time-to-Buy-and-Sell-Stock-II]({{< relref "/ChapterFour/0122.Best-Time-to-Buy-and-Sell-Stock-II.md" >}})

0 commit comments

Comments
 (0)