Cookbook add 0118 solution

This commit is contained in:
YDZ
2020-08-19 08:34:16 +08:00
parent 2c2ca375d9
commit c830ce27af
5 changed files with 93 additions and 8 deletions

View File

@ -0,0 +1,60 @@
# [118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
## 题目
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
**Note:** In Pascal's triangle, each number is the sum of the two numbers directly above it.
**Example:**
```
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
```
## 题目大意
给定一个非负整数 numRows生成杨辉三角的前 numRows 行。在杨辉三角中,每个数是它左上方和右上方的数的和。
## 解题思路
- 给定一个 n要求打印杨辉三角的前 n 行。
- 简单题。按照杨辉三角的生成规则循环打印即可。
## 代码
```go
package leetcode
func generate(numRows int) [][]int {
result := [][]int{}
for i := 0; i < numRows; i++ {
row := []int{}
for j := 0; j < i+1; j++ {
if j == 0 || j == i {
row = append(row, 1)
} else if i > 1 {
row = append(row, result[i-1][j-1]+result[i-1][j])
}
}
result = append(result, row)
}
return result
}
```

View File

@ -125,6 +125,7 @@ headless: true
- [0112.Path-Sum]({{< relref "/ChapterFour/0112.Path-Sum.md" >}})
- [0113.Path-Sum-II]({{< relref "/ChapterFour/0113.Path-Sum-II.md" >}})
- [0114.Flatten-Binary-Tree-to-Linked-List]({{< relref "/ChapterFour/0114.Flatten-Binary-Tree-to-Linked-List.md" >}})
- [0118.Pascals-Triangle]({{< relref "/ChapterFour/0118.Pascals-Triangle.md" >}})
- [0120.Triangle]({{< relref "/ChapterFour/0120.Triangle.md" >}})
- [0121.Best-Time-to-Buy-and-Sell-Stock]({{< relref "/ChapterFour/0121.Best-Time-to-Buy-and-Sell-Stock.md" >}})
- [0122.Best-Time-to-Buy-and-Sell-Stock-II]({{< relref "/ChapterFour/0122.Best-Time-to-Buy-and-Sell-Stock-II.md" >}})