题目描述:
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
n<=39
解法:
public class Solution {
public int Fibonacci(int n) {
int pre = 0;
int cur = 1;
if(n <= 1) return n;
while (n-1 > 0){
cur = pre + cur;
pre = cur - pre;
n--;
}
return cur;
}
}
题目详解参考我的另一篇文章
leetcode刷题 斐波那契数列
来源:https://blog.csdn.net/weixin_43105156/article/details/102760840