Skip to content

Commit e14d782

Browse files
committed
Pascal's Triangle
1 parent 9a93051 commit e14d782

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

118-pascals-triangle.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/pascals-triangle/
3+
4+
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
5+
In Pascal's triangle, each number is the sum of the two numbers directly above it.
6+
7+
Example:
8+
Input: 5
9+
Output:
10+
[
11+
[1],
12+
[1,1],
13+
[1,2,1],
14+
[1,3,3,1],
15+
[1,4,6,4,1]
16+
]
17+
"""
18+
class Solution(object):
19+
def generate(self, numRows):
20+
"""
21+
:type numRows: int
22+
:rtype: List[List[int]]
23+
"""
24+
if numRows == 1:
25+
return [[1]]
26+
res = []
27+
for i in range(numRows):
28+
row = [1]*(i+1)
29+
res.append(row)
30+
for j in range(1,len(row)-1):
31+
res[i][j] = res[i-1][j-1]+res[i-1][j]
32+
return res

0 commit comments

Comments
 (0)