zoukankan      html  css  js  c++  java
  • climbStairs

    假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
    
    每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
    
    注意:给定 n 是一个正整数。
    
    示例 1:
    
    输入: 2
    输出: 2
    解释: 有两种方法可以爬到楼顶。
    1.  1 阶 + 1 阶
    2.  2 阶
    
    
    // 斐波那契数
    // 记忆性迭代
    class Solution {
       public int climbStairs(int n){
           int[] num = new int[n+1];
           return climbStair(n, num);
       }
       public int climbStair(int n, int[] num) {
            if(n == 1) return 1;
            if(n == 2) return 2;
            if(n == 3) return 3;
            if(num[n] != 0){
                return num[n];
            }
            num[n] = climbStair(n-1, num) + climbStair(n-2, num);
            return num[n];
        }
    }
    
    // 常微分方程 解法
    // https://leetcode-cn.com/problems/climbing-stairs/solution/pa-lou-ti-by-leetcode/
    public class Solution {
        public int climbStairs(int n) {
            double sqrt5=Math.sqrt(5);
            double fibn=Math.pow((1+sqrt5)/2,n+1)-Math.pow((1-sqrt5)/2,n+1);
            return (int)(fibn/sqrt5);
        }
    }
    
    
  • 相关阅读:
    bzoj1221
    hdu3377
    bzoj3930
    bzoj3976
    bzoj4237
    fzu1977
    hdu1693
    ural1519
    bzoj1264
    回答自己的提问
  • 原文地址:https://www.cnblogs.com/athony/p/13036844.html
Copyright © 2011-2022 走看看