前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode 70 Climbing Stairs

Leetcode 70 Climbing Stairs

作者头像
triplebee
发布2018-01-12 15:00:11
4000
发布2018-01-12 15:00:11
举报

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?

每次爬一到两步,到终点有多少种办法?

简单DP,往后1步2步转移。

class Solution {
public:
    int climbStairs(int n) {
        vector<int> dp(n+1,0);
        dp[0]=1;
        for(int i=0;i<=n;i++)
        {
            if(i+1<=n) dp[i+1]+=dp[i];
            if(i+2<=n) dp[i+2]+=dp[i];
        }
        return dp[n];
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-09-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档