• Lc70_爬楼梯


    //假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 
    //
    // 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 
    //
    // 注意:给定 n 是一个正整数。 
    //
    // 示例 1: 
    //
    // 输入: 2
    //输出: 2
    //解释: 有两种方法可以爬到楼顶。
    //1.  1 阶 + 1 阶
    //2.  2 阶 
    //
    // 示例 2: 
    //
    // 输入: 3
    //输出: 3
    //解释: 有三种方法可以爬到楼顶。
    //1.  1 阶 + 1 阶 + 1 阶
    //2.  1 阶 + 2 阶
    //3.  2 阶 + 1 阶
    // 
    // Related Topics 动态规划
    
    package leetcode.editor.cn;
    //Java:爬楼梯
    public class P70ClimbingStairs{
        public static void main(String[] args) {
            Solution solution = new P70ClimbingStairs().new Solution();
            // TO TEST
            System.out.println(solution.climbStairs(1));
        }
        //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int climbStairs(int n) {
    //        if(n==1){
    //            return 1;
    //        }else if(n==2){
    //            return 2;
    //        }else{
    //            return climbStairs(n-2)+climbStairs(n-1);
    //        }
            int[] res = new int[n+1];
            res[0]=1;
            res[1]=2;
            if(n==1){
                return res[0];
            }
            if(n==2){
                return res[1];
            }
            for (int i =2; i <n ; i++) {
                res[i]=res[i-1]+res[i-2];
            }
            return res[n-1];
    
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)
    
    }
    
  • 相关阅读:
    三十七、Java基础之JDBC
    三十六、Java基础之File类
    各种IoC框架下实现AOP
    Eclipse导出可执行Jar文件(包含第三方Jar包)
    设计模式(Patterns in Java)-解道
    MyBatis入门示例
    freemarker实例2
    freemarker小例子
    MyEclipse8.6 破解以及注册码
    myeclipse中java文件中文注释乱码问题
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/13331331.html
Copyright © 2020-2023  润新知