118. Pascal's Triangle
https://leetcode.com/problems/pascals-triangle/
js
/**
* @param {number} numRows
* @return {number[][]}
*/
function generate(numRows) {
const result = []
for (let i = 0; i < numRows; i++) {
result.push([])
for (let j = 0; j < i + 1; j++) {
if (j === 0 || j === i) {
result[i].push(1)
} else {
result[i].push(result[i - 1][j - 1] + result[i - 1][j])
}
}
}
return result
}
py
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
result = []
for i in range(numRows):
result.append([])
for j in range(i + 1):
if j in (0, i):
result[i].append(1)
else:
result[i].append(result[i - 1][j - 1] + result[i - 1][j])
return result
go
func generate(numRows int) [][]int {
result := [][]int{}
for i := 0; i < numRows; i++ {
row := []int{}
for j := 0; j < i+1; j++ {
if j == 0 || j == i {
row = append(row, 1)
} else {
row = append(row, result[i-1][j-1]+result[i-1][j])
}
}
result = append(result, row)
}
return result
}