- 题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1)。
n<=39
- 思路
和跳台阶类似
https://blog.youkuaiyun.com/gtjsjj/article/details/106707272
- 代码
public class Solution {
public int Fibonacci(int n) {
if(n == 0)
return 0;
if(n == 1)
return 1;
int one = 0;
int two = 1;
int three = 0;
for(int i = 2; i <= n; i++){
three = one + two;
one = two;
two = three;
}
return three;
}
}