Skip to content

Commit 20ebbbf

Browse files
committed
update
1 parent e206e6c commit 20ebbbf

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

39. 杨辉三角.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
***给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。***
2+
3+
![algo22](./images/algo22.jpg)
4+
5+
```
6+
class Solution:
7+
def generate(self, numRows: int) -> List[List[int]]:
8+
#观察一下规律,发现当前一行只比上一行多了一个元素,最最关键的一点:本行元素等于上一行元素往后错一位再逐个相加
9+
res = [[1]]
10+
while len(res) < numRows:
11+
newRow = [ a+b for a,b in zip(res[-1]+[0], [0]+res[-1])]
12+
res.append(newRow)
13+
return res
14+
```

images/algo22.jpg

12 KB
Loading

0 commit comments

Comments
 (0)