剑指offer--斐波那契数列

xiaoxiao2021-02-28  93

题目描述

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。 分类:数组 解法1:根据前两项计算第三项,然后更新前两项即可。 [java]  view plain  copy public class Solution {       public int Fibonacci(int n) {           if(n==0return 0;           if(n==1||n==2return 1;           int pre  = 1;           int next = 1;           int t = 0;           int i =2;           while(i<n){               t = pre+next;               pre = next;               next = t;               i++;           }                  return next;       }   }  

原文链接  http://blog.csdn.net/crazy__chen/article/details/44986495

转载请注明原文地址: https://www.6miu.com/read-96683.html

最新回复(0)