LeetCode-118. Pascal's Triangle

问题描述

Given an integer numRows, return the first numRows of Pascal’s triangle.

Example :

1
2
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Constraints:

  • 1 <= numRows <= 30

解答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
* @Description: 118. Pascal's Triangle
* @Author: libk
* @Github: https://github.com/libk
*/
/**
* @param {number} numRows
* @return {number[][]}
*/
const generate = function (numRows) {
const res = []
for (let i = 0; i < numRows; i++) {
const row = new Array(i + 1).fill(1)
for (let j = 1; j < row.length - 1; j++) {
row[j] = res[i - 1][j - 1] + res[i - 1][j]
}
res.push(row)
}
return res
}