Created
April 6, 2017 16:46
-
-
Save Phongtlam/6e06e1ba6935dfac0a64ec82fdf4c40b to your computer and use it in GitHub Desktop.
Brute Force - Iterative way to solve Pascal (passed all tests)
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
/** | |
* @param {number} numRows | |
* @return {number[][]} | |
*/ | |
var generate = function(numRows) { | |
if (numRows === 0) { | |
return []; | |
} | |
if (numRows === 1) { | |
return [[1]]; | |
} | |
var ans = [[1], [1,1]]; | |
for (var row = 2; row < numRows; row++){ | |
ans[row] = [1]; | |
for (var col = 1; col < row; col++){ | |
ans[row][col] = ans[row-1][col] + ans[row-1][col-1]; | |
ans[row].push(1); | |
} | |
} | |
return ans; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment