Categories
Array Leetcode Loop

Pascal Triangle

let generate = function(numRows) {

if (numRows === 0) return [];

let result = [];
for (let i = 1; i <= numRows;i++) {
let row = [];
for (let j = 0; j < i; j++) {
if (j === 0 || j === i - 1) {
row[j] = 1;
}
else {
row[j] = result[i - 2][j - 1] + result[i - 2][j];
}
}
result.push(row);
}

return result;
};

Leave a Reply

Your email address will not be published. Required fields are marked *