Browse Source

动态规划 斐波那契数列

master
YuJian 3 years ago
parent
commit
b475e657c4
  1. 0
      Dynamic programming/Fibonacci.test.ts
  2. 25
      Dynamic programming/Fibonacci_Cycle.ts
  3. 8
      Dynamic programming/Fibonacci_Recursion.ts

0
Dynamic programming/Fibonacci.test.ts

25
Dynamic programming/Fibonacci_Cycle.ts

@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
const Fibonacci_Cycle = (n: number): number => {
if (n <= 0) return 0;
if (n === 1) return n;
let n1 = 0;
let n2 = 0;
let n3 = 0;
for (let index = 0; index <= n; index++) {
if (index === 1) {
n1 = 0;
n2 = 1;
continue;
}
n3 = n1 + n2;
n1 = n2;
n2 = n3;
}
return n3;
};
console.log(Fibonacci_Cycle(6));

8
Dynamic programming/Fibonacci_Recursion.ts

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
const Fibonacci_Recursion = (n: number): number => {
if (n <= 0) return 0;
if (n === 1) return n;
return Fibonacci_Recursion(n - 1) + Fibonacci_Recursion(n - 2);
};
console.log(Fibonacci_Recursion(80));
Loading…
Cancel
Save