算法题LC79:climbing-stairs

动态规划:
题目描述
你在爬楼梯,需要n步才能爬到楼梯顶部
每次你只能向上爬1步或者2步。有多少种方法可以爬到楼梯顶部?

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?
输入描述

输出描述

示例1:
输入

输出
        
代码:

public class Solution {
public int climbStairs(int n) {
        int[] dp = new int[n + 1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 2; i < dp.length; i ++ ) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }
}
发布了73 篇原创文章 · 获赞 1 · 访问量 1360

猜你喜欢

转载自blog.csdn.net/alidingding/article/details/104673677