-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascal's Triangle.js
More file actions
38 lines (34 loc) · 723 Bytes
/
Copy pathPascal's Triangle.js
File metadata and controls
38 lines (34 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]*/
/**
* @param {number} numRows
* @return {number[][]}
*/
var generate = function(numRows) {
if(numRows == 0)
return []
if(numRows == 1)
return [[1]]
var arr = [[1]]
i = 1
return generateHelper(arr,i,numRows)
};
var generateHelper = function(arr,i,numRows){
arr[i] = [arr[i-1][0]]
var j = 1
for(; j < arr[i-1].length; j++)
arr[i][j] = arr[i-1][j-1] + arr[i-1][j]
arr[i][j] = arr[i-1][j-1]
if(i+1 == numRows)
return arr
else
return generateHelper(arr,i+1,numRows)
}