已知条件:
f(n) = {
0 n = 0
1 n = 1
f(n-1)+f(n-2) n > 1
}
求f(n)的值。
private static int fibonacci(int n) {
if (n < 2) {
return n;
}
int fibOne = 1;
int fibTow = 0;
int fib = 0;
for (int i = 2; i <= n; i++) {
fib = fibOne + fibTow;
fibTow = fibOne;
fibOne = fib;
}
return fib;
}
笔记:
从下到上倒推结果