题目:

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

思路:

到达当前台阶n,是由从n-1台阶走一步来到或者从n-2台阶走两步来到

package dp;

public class ClimbingStairs {

    public int climbStairs(int n) {
        int[] dp = new int[n + 1];
        dp[0] = dp[1] = 1;
        for (int i = 2; i <= n; ++i) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        
        return dp[n];
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ClimbingStairs c = new ClimbingStairs();
        System.out.println(c.climbStairs(4));
    }

}

 

相关文章:

  • 2021-09-03
  • 2021-05-30
  • 2021-08-12
  • 2021-05-16
  • 2021-12-01
  • 2022-12-23
猜你喜欢
  • 2021-06-30
  • 2021-09-06
  • 2021-11-06
  • 2022-02-03
  • 2021-12-19
  • 2022-01-23
相关资源
相似解决方案