剑指Offer-9
来源:互联网 发布:温度监控软件 贴吧 编辑:程序博客网 时间:2024/06/07 07:52
题目:
写一个函数,输入 n ,求斐波那契数列的第n项
实现
public class Solution9 { //直接递归,,当递归深度较深时,效率很低 public int Fibonacci1(int n){ if(n<0){ throw new RuntimeException("n 小于 0,输入不合法"); } if(n<2){ return n; } return Fibonacci1(n-1)+Fibonacci1(n-2); } public int Fibonacci2(int n){ int[] result={0,1}; if(n<0){ throw new RuntimeException("n 小于 0,输入不合法"); } if(n<2){ return result[n]; } int fibo_one = 0; int fibo_two = 1; int fibo_n=0; for(int i=2;i<=n;i++){ fibo_n = fibo_one+fibo_two; fibo_one = fibo_two; fibo_two = fibo_n; } return fibo_n; } public static void main(String[] args){ int n = 9; Solution9 s9 = new Solution9(); int result1 = s9.Fibonacci1(n); int result2 = s9.Fibonacci2(n); System.out.println(result1); System.out.println(result2); }}
阅读全文
1 0
- 剑指OFFER---9
- 剑指Offer-9
- 剑指offer面试题9
- 剑指offer 面试题9
- 剑指offer 面试题9
- 剑指Offer-9-斐波拉契数列
- 【面试题】剑指offer 9
- 剑指OFFER
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 利用ViewPager实现画廊Gallery效果
- http协议
- 密码箱
- Hbase的Filter详解使用
- android权限(permission)大全
- 剑指Offer-9
- audition CS6.0 无法打开.aac 文件和 .3gpp文件问题解决
- 用pycaffe训练人工神经网络步骤
- 操作系统(0)
- 如果做双击按键功能
- NV_INIT和NV_RESTORE的区别
- 关于Eclipse 64 bit里SVN报Subclipse talks to Subversion via a Java API that requires access to native错
- GET,POST——简述小全
- 算法练习-回型矩阵