剑指offer--斐波那契数列

题目描述

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。

分类:数组

解法1:根据前两项计算第三项,然后更新前两项即可。
[java]  view plain  copy
  1. public class Solution {  
  2.     public int Fibonacci(int n) {  
  3.         if(n==0return 0;  
  4.         if(n==1||n==2return 1;  
  5.         int pre  = 1;  
  6.         int next = 1;  
  7.         int t = 0;  
  8.         int i =2;  
  9.         while(i<n){  
  10.             t = pre+next;  
  11.             pre = next;  
  12.             next = t;  
  13.             i++;  
  14.         }         
  15.         return next;  
  16.     }  
  17. }  


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值