Skip to content

Commit cbe2842

Browse files
committed
feat: pascals-triangle
1 parent ce002de commit cbe2842

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

arr.pascals-triangle.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""
6+
118. 杨辉三角
7+
https://leetcode-cn.com/problems/pascals-triangle/
8+
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
9+
"""
10+
def generate(self, numRows: int) -> List[List[int]]:
11+
res = []
12+
i = 0
13+
while i < numRows:
14+
if i == 0:
15+
res.append([1])
16+
i += 1
17+
continue
18+
19+
tmp = []
20+
j = 0
21+
while j <= i:
22+
if j >= len(res[i - 1]) or j == 0:
23+
tmp.append(1)
24+
else:
25+
tmp.append(res[i - 1][j] + res[i - 1][j - 1])
26+
27+
j += 1
28+
29+
res.append(tmp)
30+
i += 1
31+
32+
return res
33+
34+
35+
so = Solution()
36+
print(so.generate(5))

0 commit comments

Comments
 (0)