Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
func generate(numRows int) [][]int {
var rows[][] int = make([][]int, numRows)
for i:=0;i<numRows;i++ {
var cols[] int = make([]int, i+1)
for j:=0;j<=i;j++ {
if i==0 || j==0 {
cols[j] = 1
} else if i == j {
cols[j] = 1
} else {
cols[j] = rows[i-1][j-1] + rows[i-1][j]
}
}
rows[i] = cols
}
return rows
}
class Solution:
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
pascalList = []
for i in range(0,numRows):
innerList = []
for j in range(0,i+1):
if(i == 0 or j == 0):
innerList.append(1)
elif(i==j):
innerList.append(1)
else:
innerList.append(pascalList[i-1][j-1] + pascalList[i-1][j])
pascalList.append(innerList)
return pascalList