实现:

public class Solution {
    /**
     * @param n: an integer
     * @return: an ineger f(n)
     */
    public int fibonacci(int n) {
        // write your code here
        
        return foo(n);
     
    }
    
    public int foo(int x){
        
        if(x == 1){
            return 0;
        }
        else if(x == 2){
            return 1;
        }
        
        return foo(x-2) + foo(x - 1);
    }
}

提交时提示:

Time Limit Exceeded

你的代码运行时间超过了限制,检查你的时间复杂度。TLE通常是由死循环造成的,思考一下你的时间复杂度是否是最优的。

 

求最优算法!

相关文章:

  • 2021-10-22
  • 2022-12-23
  • 2022-01-28
  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
猜你喜欢
  • 2021-08-12
  • 2021-12-23
  • 2021-09-12
  • 2021-11-28
  • 2022-12-23
  • 2021-07-01
  • 2021-09-28
相关资源
相似解决方案