Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] main from itcharge:main #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 18 commits into AlgorithmAndLeetCode:main from itcharge:main
Jul 21, 2022
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
9bea1cd
更新题解列表
itcharge Jul 21, 2022
9627f49
更新章节列表
itcharge Jul 21, 2022
8127501
Update 0035. 搜索插入位置.md
itcharge Jul 21, 2022
6fe2fa9
Update 0004. 寻找两个正序数组的中位数.md
itcharge Jul 21, 2022
523d068
Update 0128. 最长连续序列.md
itcharge Jul 21, 2022
2f0f572
Update 0215. 数组中的第K个最大元素.md
itcharge Jul 21, 2022
6702825
Update 0303. 区域和检索 - 数组不可变.md
itcharge Jul 21, 2022
79bf079
Update 0307. 区域和检索 - 数组可修改.md
itcharge Jul 21, 2022
a48c8fd
Update 0370. 区间加法.md
itcharge Jul 21, 2022
75de141
Update 0715. Range 模块.md
itcharge Jul 21, 2022
afde623
Update 0912. 排序数组.md
itcharge Jul 21, 2022
6304f12
Update 1099. 小于 K 的两数之和.md
itcharge Jul 21, 2022
5c18ff7
Update 1109. 航班预订统计.md
itcharge Jul 21, 2022
c1f4f6d
Update 1310. 子数组异或查询.md
itcharge Jul 21, 2022
23362c9
Update 剑指 Offer II 119. 最长连续序列.md
itcharge Jul 21, 2022
5e64245
Update 0300. 最长递增子序列.md
itcharge Jul 21, 2022
4b7e493
Update 0118. 杨辉三角.md
itcharge Jul 21, 2022
df8e274
Update 0118. 杨辉三角.md
itcharge Jul 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 0118. 杨辉三角.md
  • Loading branch information
itcharge committed Jul 21, 2022
commit 4b7e493a92b870d565a2d19bbced8308610b277a
72 changes: 61 additions & 11 deletions Solutions/0118. 杨辉三角.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,73 @@

## 题目大意

给定一个整数 n,生成前 n 行的杨辉三角
**描述**:给定一个整数 `n`

例如 n = 5:
**要求**:生成前 `n` 行的杨辉三角。

**说明**:

- 1ドル \le numRows \le 30$。

**示例**:

```Python
输入 numRows = 5
输出 [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
```
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
```

![](https://pic.leetcode-cn.com/1626927345-DZmfxB-PascalTriangleAnimated2.gif)

## 解题思路

两重循环遍历。先遍历 n 行,再对每一行每个位置上的元素进行赋值计算。每一行两侧元素赋值为 1,中间元素为上一行两个元素相加。
### 思路 1:动态规划

###### 1. 划分阶段

按照行数进行阶段划分。

###### 2. 定义状态

定义状态 `dp[i][j]` 为:杨辉三角第 `i` 行、第 `j` 列位置上的值。

###### 3. 状态转移方程

根据观察,很容易得出状态转移方程为:`dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]`,此时 `i > 0,j > 0`。

###### 4. 初始条件

- 每一行第一列都为 `1`,即 `dp[i][0] = 1`。
- 每一行最后一列都为 `1`,即 `dp[i][i] = 1`。

###### 5. 最终结果

根据题意和状态定义,我们将每行结果存入答案数组中,将其返回。

### 思路 1:动态规划代码

```Python
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
dp = [[0] * i for i in range(1, numRows + 1)]

for i in range(numRows):
dp[i][0] = 1
dp[i][i] = 1

res = []
for i in range(numRows):
for j in range(i):
if i != 0 and j != 0:
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
res.append(dp[i])

return res
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n^2)$。初始条件赋值的时间复杂度为 $O(n),ドル两重循环遍历的时间复杂度为 $O(n^2),ドル所以总的时间复杂度为 $O(n^2)$。
- **空间复杂度**:$O(n^2)$。用到了二维数组保存状态,所以总体空间复杂度为 $O(n^2)$。

## 代码

Expand Down

AltStyle によって変換されたページ (->オリジナル) /