1. How I sloved
I made all elements ‘1’ in each line. The elements except the first and last from the third row are the sum of the elements in the previous row. So I added the former elements, and it became an element of a new row, and added the new row to the list. When the number of rows was equal to the input, I returned the list.
2. Code
class Solution:
def generate(self, numRows: int) -> List[List[int]]:
triangle = []
for i in range(1,numRows+1):
row = [1 for _ in range(i)]
if i>2:
for j in range(1,i-1):
row[j]=triangle[i-2][j-1]+triangle[i-2][j]
triangle.append(row)
return triangle
3. Result
Runtime : 20 ms(98.68%), Memory usage : 14.1 MB(99.98%)
(Runtime can be different by a system even if it is a same code.)