描述
大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項。
解法
不能用遞歸,會超時滴。
code
class Solution {
public:
int Fibonacci(int n){
if (n==0 || n==1)
return n;
int a = 0, b=1, tmp;
for(int i=2; i<=n; i++){
tmp = a+b;
a = b;
b = tmp;
}
return tmp;
}
};