8、跳台阶

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NCUscienceZ/article/details/84315348

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
标准优化版、

import java . util . * ;
public class Solution {
    public Map<Integer, Integer> arr= new HashMap<>();
    public int JumpFloor(int target) {
            if (target == 1) return 1;
            if (target == 2) return 2;
            if (arr.containsKey(target)) return arr.get(target);
            arr.put(target, JumpFloor(target-1)+JumpFloor(target-2));
            return arr.get(target);
    }
}

牛客网数据太水…不记忆化处理都能过。。。。。。

import java . util . * ;
public class Solution {
    public int JumpFloor(int target) {
        if (target == 1) return 1;
        if (target == 2) return 2;
        return JumpFloor(target-1)+JumpFloor(target-2);
    }
}

本题有暴露了一个语法细节,java集合元素只能是对象类型,Map<int, int>典型报错。

猜你喜欢

转载自blog.csdn.net/NCUscienceZ/article/details/84315348
今日推荐