【未通过】LintCode #366 斐波纳契数列

实现:

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通常是由死循环造成的,思考一下你的时间复杂度是否是最优的。

求最优算法!

猜你喜欢

转载自www.cnblogs.com/icebutterfly/p/8950285.html
今日推荐