LC0118 - Pascal’s Triangle (EASY)#
This is the solution for Leetcode Problem 118 - Pascal’s Triangle.
1"""Solution to Leetcode 118 - 118
2
3Link : https://leetcode.com/problems/pascals-triangle/
4Level: easy
5"""
6
7
8class Solution:
9 def generate(self, numRows: int) -> List[List[int]]:
10 result = []
11 for index in range(1, numRows + 1):
12 if index == 1:
13 result.append([1])
14 elif index == 2:
15 result.append([1, 1])
16 else:
17 current_row = [0 for _ in range(index)]
18 current_row[0] = current_row[-1] = 1
19 for j in range(1, index - 1):
20 previous_row = result[-1]
21 current_row[j] = previous_row[j - 1] + previous_row[j]
22
23 result.append(current_row)
24
25 return result