class Solution {
public:
int Fibonacci(int n) {
if(n==0)return 0;
if(n==1)return 1;
int a=0,b=1,temp=0;
while(n>1){
temp=a+b;
a=b;
b=temp;
n--;
}
return b;
}
};
或者:
class Solution {
public:
int Fibonacci(int n) {
int f = 0, g = 1;
while(n--) {
g += f;
f = g - f;
}
return f;
}
};