编程语言
首页 > 编程语言> > 算法题LC79:climbing-stairs

算法题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];
    }
}

标签:int,top,steps,climbing,LC79,stairs,public,dp
来源: https://blog.csdn.net/alidingding/article/details/104673677