题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
n<=39
【分析】动态规划的最简单题目:
class Solution {
public:
int Fibonacci(int n) {
int first = 0;
int second = 1;
int result = n;
for(int i = 2; i <= n; ++i){
result = first + second;
first = second;
second = result;
}
return result;
}
};