Skip to content

Commit 5edcdc5

Browse files
committed
[0212]ADD:Lc-119
1 parent 22e32ad commit 5edcdc5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [Description](https://leetcode-cn.com/problems/pascals-triangle-ii/)
2+
给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
3+
4+
![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
5+
6+
在杨辉三角中,每个数是它左上方和右上方的数的和。
7+
8+
示例:
9+
10+
输入: 3
11+
输出: [1,3,3,1]
12+
进阶:
13+
14+
你可以优化你的算法到 O(k) 空间复杂度吗?
15+
16+
17+
# Solution
18+
- 时间复杂度:$O(n^2)$
19+
- 空间复杂度:$O(n)$
20+
21+
```python
22+
class Solution:
23+
def getRow(self, rowIndex: int) -> List[int]:
24+
dp = [1]*(rowIndex+1)
25+
26+
for i in range(2, rowIndex+1):
27+
tmp_dp = dp[:i]
28+
for j in range(1, i):
29+
dp[j] = tmp_dp[j-1] + tmp_dp[j]
30+
31+
return dp
32+
```
33+

0 commit comments

Comments
 (0)