https://leetcode.com/problems/pascals-triangle/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @param {number} numRows
* @return {number[][]}
*/
var generate = function(numRows) {
let res = []
for (let i = 1; i <= numRows; i++) {
if (i === 1) {
res.push([1])
}
if (i === 2) {
res.push([1, 1])
}
if (i > 2) {
let tmp = [1]
for (let j = 1; j <= i - 2; j++) {
tmp.push(res[i - 2][j - 1] + res[i - 2][j])
}
tmp.push(1)
res.push(tmp)
}
}
return res
};