-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathpascalTriangleRecursive.js
More file actions
31 lines (24 loc) · 1.1 KB
/
Copy pathpascalTriangleRecursive.js
File metadata and controls
31 lines (24 loc) · 1.1 KB
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
/**
* @param {number} lineNumber - zero based.
* @return {number[]}
*/
export default function pascalTriangleRecursive(lineNumber) {
if (lineNumber === 0) {
return [1];
}
const currentLineSize = lineNumber + 1;
const previousLineSize = currentLineSize - 1;
// Tạo vùng chứa cho các giá trị ở dòng hiện tại.
const currentLine = [];
// Ta sẽ tính toán dòng hiện tại dựa trên dòng trước đó.
const previousLine = pascalTriangleRecursive(lineNumber - 1);
// Xem tất cả các phần tử của dòng hiện tại ngoại trừ phần tử đầu tiên và
// phần tử cuối cùng (vì chúng luôn bằng 1) và tính toán
// hệ số hiện tại dựa trên dòng trước đó.
for (let numIndex = 0; numIndex < currentLineSize; numIndex += 1) {
const leftCoefficient = (numIndex - 1) >= 0 ? previousLine[numIndex - 1] : 0;
const rightCoefficient = numIndex < previousLineSize ? previousLine[numIndex] : 0;
currentLine[numIndex] = leftCoefficient + rightCoefficient;
}
return currentLine;
}