杨辉三角 II-简单
难度:简单
题目描述:
给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 *k *行。
示例:
输入: 3;
输出: [1, 3, 3, 1];
1
2
2
解题思路:
同杨辉三角一样的思路 ```javascript var getRow = function(n) { let i = 0; let data = []; let pre = 0; let cur = 0; while (i < n + 1) { let arr = []; if (i === 0) { arr[i] = 1; } else { for (let j = 0; j < i + 1; j++) { pre = data[i - 1][j - 1] || 0; cur = data[i - 1][j] || 0; arr[j] = pre + cur; } } data.push(arr); i++; } return data[n]; }; ```
公式法
var getRow = function (rowIndex) {
let curr = 1;
const ary = [];
for (let i = 0; i <= rowIndex; i++) {
ary[i] = curr;
curr = (curr * (rowIndex - i)) / (i + 1);
}
return ary;
};
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
← Fizz Buzz-简单 杨辉三角-简单 →