剑指offer 面试题9 斐波那契数列 java版答案
2016-11-15 17:25
597 查看
package OfferAnswer; /** * 面试题09 * 斐波那契数列 * @author lwk * */ public class Answer09 { public static void main(String[] args) { long start = System.currentTimeMillis(); System.out.println(Fibonacci1(40)); long end = System.currentTimeMillis(); System.out.println(end - start); } //递归解法 时间复杂度指数递增 public static long Fibonacci1(int n){ if(n == 0){ return 0; } if(n == 1){ return 1; } return Fibonacci1(n-1) + Fibonacci1(n-2); } //时间复杂度O(n)的解法 public static long Fibonacci2(int n){ if(n == 0){ return 0; } if(n == 1){ return 1; } long first = 0; long second = 1; long result = 0; for (int i = 2; i <= n; i++) { result = first + second; first = second; second = result; } return result; } }
相关文章推荐
- 剑指offer 面试题3 二维数组中的查找 java版答案
- 剑指offer 面试题4 替换空格 java版答案
- 九度剑指Offer面试题9:斐波那契数列(Java题解)
- 剑指Offer面试题9(java版)斐波那契数列
- 剑指offer 面试题5 从尾到头打印链表 java版答案
- <剑指offer 面试题10_1>斐波那契数列 Java
- 剑指offer 面试题6 重建二叉树 java版答案
- 剑指offer--面试题9:斐波那契数列--Java实现
- 剑指offer--面试题10:二进制中1的个数--Java实现
- 剑指offer面试题6 重建二叉树(java)
- 剑指offer面试题 java实现版 面试题2 实现Singleton模式
- 剑指offer面试题3 二维数组中的查找 (java)
- 剑指offer--面试题7:用两个栈实现队列--Java实现
- 剑指offer面试题5 从头到尾打印链表(java)
- 【剑指offer】面试题3:二位数组中的查找 java
- 剑指offer--面试题12:打印1到最大的n位数--Java实现
- 剑指offer--面试题13:在O(1)时间删除链表结点--Java实现
- 剑指offer面试题4 替换空格(java)
- 剑指offer-面试题9-斐波那契数列(Fibonacci)
- 剑指offer-面试题7:俩个栈实现队列(java)