Fibonacci Number
Fib数, 当前数等于前两个数的和, 需要记录一下前两个数即可.
class Solution {
public int fib(int n) {
if(n == 0)
return 0;
if(n == 1)
return 1;
int first = 0;
int second = 1;
int current = 1;
for(int i = 2; i <= n; i++) {
current = first + second;
first = second;
second = current;
}
return current;
}
}