题目
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
思路
我一开始想用递归,但是想起动态规划可以更节省资源更快的解决问题
代码全称只需要几个变量,不用递归栈
收获
动态规划
代码
class Solution {
public int fib(int n) {
int a = 0, b = 1, sum;
for(int i = 0; i < n; i++){
sum = (a + b) % 1000000007;
a = b;
b = sum;
}
return a;
}
}