zoukankan      html  css  js  c++  java
  • 《面试题精选》15.O(logn)求Fibonacci数列

    题目:定义Fibonacci数列例如以下:

            /    0                      n=0
    f(n)=      1                      n=1
                f(n-1)+f(n-2)          n=2

    输入n,用最快的方法求该数列的第n项。

    分析:刚看到这道题的时候,还以为怎么会有这么简单,汗,原来理所当然的使用的递归,时间复杂度太大,看看以下这段递归代码。

    public static int fun(int n){
    	if(n==0) return 0 ;
    	else if(n==1) return 1 ;
    	else return (fun(n-1)+fun(n-2)) ;
    }
    大家来分析下,比方要求f(10),那么他的 运算步骤例如以下图:

     f(10)
                   /        
                f(9)         f(8)
              /            /    
           f(8)     f(7)  f(7)   f(6)
          /        /   
     
       f(7)  f(6)  f(6) f(5)

    从上面的树状结构中就能够知道利用递归的方法会有很多反复的节点,并且n越大反复节点越多。他的时间复杂度是以n的指数的方式递增的。

    那么要做的优化,第一个想到的就是去除反复,怎样去除呢?我们能够从f(0),f(1)開始运算,利用循环来按顺序求出f(n)。这种时间复杂度是O(n),代码例如以下

    public static int fun1(int n){
    	int fib1 = 0 ;
    	int fib2 = 1 ;
    	int fibsum = 0 ;
    	if(n<0) System.out.println("error") ;
    	else if(n==0) return 0 ;
    	else if(n==1) return 1 ;
    	else{
    		for(int i=1;i<n;i++){
    			fibsum = fib1 + fib2 ;
    			fib1 = fib2 ;
    			fib2 = fibsum ;
    		}
    	}
    	return fibsum ;
    }

    总结:谨慎使用递归。

  • 相关阅读:
    [LeetCode] 769. Max Chunks To Make Sorted
    [LeetCode] 563. Binary Tree Tilt
    [LeetCode] 1802. Maximum Value at a Given Index in a Bounded Array
    [LeetCode] 1198. Find Smallest Common Element in All Rows
    [LeetCode] 370. Range Addition
    [LeetCode] 1749. Maximum Absolute Sum of Any Subarray
    [LeetCode] 1801. Number of Orders in the Backlog
    [LeetCode] 869. Reordered Power of 2
    [LeetCode] 841. Keys and Rooms
    [LeetCode] 1603. Design Parking System
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4078399.html
Copyright © 2011-2022 走看看