1class Solution:
2 def generate(self, numRows: int) -> List[List[int]]:
3
4 results = []
5
6 for row in range(1,numRows+1):
7 if row <= 2:
8 result = [1] * row
9 results.append(result)
10 continue
11
12 result = results[-1] + [1]
13 new_row = result[:]
14 for i in range(1, row-1):
15 new_row[i] = result[i-1] + result[i]
16
17 results.append(new_row)
18
19 return results
This code is a solution to the "Pascal's Triangle" problem. The function "generate" takes an integer input "numRows" and returns a 2D list representing Pascal's Triangle with "numRows" number of rows. The code starts by initializing an empty list called "results" to store each row of the triangle. It then starts a for loop that iterates "row" from 1 to "numRows". Inside the loop, there are three main scenarios: when "row" is 1 or 2, and when "row" is greater than 2. When "row" is 1 or 2, it creates a new list called "result" with all elements set to 1 using [1] * row. It then appends this "result" to the "results" list. When "row" is greater than 2, it retrieves the last row of "results" and adds an additional 1 at the end to form the new row. Then, it creates a copy of this new row called "new_row" to keep track of the current row being constructed. It then uses a nested for loop to iterate from index 1 to index row-1 of the "result" list. In each iteration, it assigns the sum of the previous row elements at index i-1 and i to the corresponding element of "new_row". After constructing "new_row", it appends it to the "results" list. Finally, it returns the "results" list representing Pascal's Triangle with "numRows" number of rows.
Solution to "Count Number of Texts" question on Leetcode.
Solution to "Second Minimum Node In a Binary Tree" on Leetcode.