118.杨辉三角
小于 1 分钟
118.杨辉三角
/*
* @lc app=leetcode.cn id=118 lang=typescript
*
* [118] 杨辉三角
*/
// @lc code=start
// 注意:递归方式生成杨辉三角在行数较大时可能会导致性能问题。因此,对于大型的杨辉三角,最好使用循环方式生成。
function generate(numRows: number): number[][] {
let row: number[][] = [];
for (let i = 0; i < numRows; i++) {
row[i] = [];
row[i][0] = 1;
for (let j = 0; j < i; j++) {
let count = row[i - 1][j - 1] + row[i - 1][j];
row[i][j] = isNaN(count) ? 1 : count;
}
row[i][i] = 1;
}
return row;
}
// @lc code=end
Loading...
