Created
May 25, 2017 16:35
-
-
Save junjunparkpark/7f7052293111923d972d0f327143c6e3 to your computer and use it in GitHub Desktop.
Pascal's Triangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var generate = function(numRows) { | |
if (numRows === 0) { return []; } | |
var pascalsTriangle = [[1]]; | |
for (let i = 1; i < numRows; i++) { | |
var last = pascalsTriangle[pascalsTriangle.length - 1]; | |
var newLevel = []; | |
for (let j = 0; j <= last.length; j++) { | |
var previous = last[j - 1]; | |
var current = last[j]; | |
if (j === 0 || j === last.length) { | |
newLevel.push(1); | |
} else { | |
newLevel.push(current + previous); | |
} | |
} | |
pascalsTriangle.push(newLevel); | |
} | |
return pascalsTriangle; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment