forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.js
36 lines (34 loc) · 933 Bytes
/
Solution.js
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
const generate = function (numRows) {
let arr = [];
for (let i = 0; i < numRows; i++) {
let row = [];
row[0] = 1;
row[i] = 1;
for (let j = 1; j < row.length - 1; j++) {
row[j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
arr.push(row);
}
return arr;
};
/**
* Author: Mcnwork2018
*/
var generate = function (numRows) {
if (numRows === 0) return [];
if (numRows === 1) return [[1]];
if (numRows === 2) return [[1], [1, 1]];
let triangleArray = [[1], [1, 1]];
for (let i = 2; i < numRows; ++i) {
triangleArray[i] = [];
for (let j = 0; j < i + 1; ++j) {
if (j === 0 || j === i) {
triangleArray[i][j] = 1;
} else {
triangleArray[i][j] =
triangleArray[i - 1][j - 1] + triangleArray[i - 1][j];
}
}
}
return triangleArray;
};