Skip to content

Instantly share code, notes, and snippets.

@kennyxcao
Last active August 31, 2017 17:04
Show Gist options
  • Save kennyxcao/3d67481f8d864369a810bea397b1a0bd to your computer and use it in GitHub Desktop.
Save kennyxcao/3d67481f8d864369a810bea397b1a0bd to your computer and use it in GitHub Desktop.
PascalTriangle LeetCode Solution
/**
* @param {number} numRows
* @return {number[][]}
*/
var generate = function(numRows) {
var result = [];
for (var i = 0; i < numRows; i++) {
var row = [];
if (i === 0) {
row.push(1);
result.push(row);
} else {
var j = 0;
while(j <= i) {
if (j === 0) {
row.push(1);
} else if (j === i) {
row.push(1);
result.push(row);
} else {
var value = result[i - 1][j - 1] + result[i - 1][j];
row.push(value);
}
j++;
}
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment