题解 | #斐波那契数列#
斐波那契数列
https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3
递归
class Solution { public: int Fibonacci(int n) { if (n == 1 || n == 2) return 1; return Fibonacci(n-1) + Fibonacci(n-2); } };
空间压缩
class Solution { public: int Fibonacci(int n) { if (n == 1 || n == 2) return 1; int a1 = 1; int a2 = 1; int a3 = 0; for (int i = 3; i <= n; ++i) { a3 = a1 + a2; a1 = a2; a2 = a3; } return a3; } };
2023-剑指-DP 文章被收录于专栏
2023-剑指-DP