Skip to content

Commit 74bad1c

Browse files
committed
add solution of 118
- 118. Pascal's Triangle https://leetcode.com/problems/pascals-triangle/
1 parent acca70d commit 74bad1c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

solutions/pascals-triangle.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
- 118. Pascal's Triangle
3+
https://leetcode.com/problems/pascals-triangle/
4+
"""
5+
6+
# Method 1
7+
class Solution:
8+
def generate(self, numRows: int) -> List[List[int]]:
9+
result = []
10+
if numRows == 0:
11+
return result
12+
result.append([1])
13+
if numRows == 1:
14+
return result
15+
for i in range(2, numRows + 1):
16+
temp = [0 for j in range(i)]
17+
for j in range(len(temp)):
18+
if j == 0:
19+
temp[j] = result[-1][0]
20+
elif j == len(temp) - 1:
21+
temp[j] = result[-1][-1]
22+
elif j != 0 and j != len(temp) - 1:
23+
newNum = result[-1][j-1] + result[-1][j]
24+
temp[j] = (newNum)
25+
result.append(temp)
26+
return result

0 commit comments

Comments
 (0)