Skip to content

Commit e7c86f8

Browse files
PURUSHOTMtechyminati
authored andcommitted
Python:LeetCode 118:Solution for Pascal's Triangle
Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly
1 parent 6056be8 commit e7c86f8

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎Pascal_triangle.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def generate(self, numRows: int) -> List[List[int]]:
3+
a = []
4+
for x in range(numRows):
5+
a.append([1])
6+
# creating the 2d array
7+
# appending the starting 1: [1, ...]
8+
9+
if x > 1:
10+
# skipping the first two rows
11+
12+
for c in range(x-1):
13+
# for loop used to find info about previous row
14+
15+
a[x].append(a[x-1][c]+a[x-1][c+1])
16+
# math for adding the c and the c+1 number from the previous row
17+
18+
if x > 0:
19+
# checking if its not the first row
20+
21+
a[x].append(1)
22+
# appending the ending 1: [1, ..., 1]
23+
24+
return a

0 commit comments

Comments
 (0)