给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
118的扩展,用了JavaScript:
/**
* @param {number} rowIndex
* @return {number[]}
*/
var getRow = function(rowIndex) {
if(rowIndex == 0){
return [1];
}
if(rowIndex == 1){
return [1,1];
}
const result = [];
if(rowIndex >= 2){
for(let i = 0; i <= rowIndex; i++){
const arr = []
for( let j = 0; j <= i; j++){
if(j > 0 && j < i){
arr[j] = result[i-1][j-1] + result[i-1][j]
}else{
arr[j] = 1
}
}
result.push(arr)
}
return result[rowIndex]; //返回第rowindex行
}
};
需要考虑rowindex为0和1的情况