Question1
/**
*
* @param numRows int整型
* @return int整型二维数组
*/
function generate( numRows ) {
if(numRows<=0) return [];
if(numRows==1) return [[1]];
if(numRows==2) return [[1],[1,1]];
var res = [[1],[1,1]]
for(var i=2;i<numRows;i++){
var temp=[];
for(var j=0;j<res[i-1].length-1;j++){
temp.push(res[i-1][j]+res[i-1][j+1])
}
temp.unshift(1);
temp.push(1);
res.push(temp);
}
return res;
}
module.exports = {
generate : generate
};Question2
k=0 返回的是第一行
/**
*
* @param rowIndex int整型
* @return int整型一维数组
*/
function getRow( rowIndex ) {
// write code here
if(rowIndex<=0) return [1];
if(rowIndex==1) return [1,1];
var res = [[1],[1,1]]
for(var i=2;i<=rowIndex;i++){
var temp=[];
for(var j=0;j<res[i-1].length-1;j++){
temp.push(res[i-1][j]+res[i-1][j+1])
}
temp.unshift(1);
temp.push(1);
res.push(temp);
}
return res[rowIndex];
}
module.exports = {
getRow : getRow
};
京公网安备 11010502036488号