-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpascalTriangle.ts
53 lines (34 loc) · 915 Bytes
/
pascalTriangle.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
118. Pascal's Triangle
Given an integer numRows, return the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1
Output: [[1]]
Constraints:
1 <= numRows <= 30
*/
const generateItem = (previousItem: number[]): number[] => {
const ans = [] as number[];
for (let i = 0; i < previousItem.length - 1; i++) {
ans.push(previousItem[i] + previousItem[i + 1]);
}
return [1, ...ans, 1];
};
export function generate(numRows: number): number[][] {
const res = [] as number[][];
if (numRows === 1) {
return [[1]];
}
if (numRows === 2) {
return [[1], [1, 1]];
}
res.push([1], [1, 1]);
for (let i = 2; i < numRows; i++) {
res.push(generateItem(res[i - 1]));
}
return res;
}