题解 | #跳台阶#

跳台阶

http://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4

此本仅用于本人记录

斐波那契数列。

public class Solution {
    public int jumpFloor(int target) {
        int[] result = new int[target+1];
        result[0] = 1;
        result[1] = 1;
        for(int i = 2;i <= target;i++){
            result[i] = result[i-1] +result[i-2];
        }
        return result[target];
    }

}

一运行,内存方面好家伙只超过4%的人。

public class Solution {
    public int jumpFloor(int target) {
        int d1 = 1,d2 = 1;
        int result = 1;
        for(int i = 2;i <= target;i++){
            result = d1 +d2;
            d1 = d2;
            d2 = result;
        }
        return result;
    }

}

一运行,好家伙!只超过1%的人!
看了下排行第一的java代码,一运行,内存方面也只超过了8%的人。。。。

public class Solution {
    public int jumpFloor(int target) {

        int d1 = 1, d2 = 1;
        while(target-- > 1 ){
            int temp = d2;
            d2 = d2 + d1;
            d1 = temp;
        }
        return d2;

    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务