70. Climbing Stairs

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?

Note: Given n will be a positive integer.

最简单的动态规划题了吧 应该是

class Solution {
    public int climbStairs(int n) {
        if(n==1)
        return 1;
        if(n==2)
        return 2;
        int[] dp = new int[n];
        dp[0]= 1;
        dp[1]= 2;
        for(int i = 2;i<n;i++ )
        {
            dp[i]=dp[i-1]+dp[i-2];
        }
        return dp[n-1];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,921评论 0 23
  • 《登泰山》 万壑无边玉树悬,明皇问鼎旧中原。 遥观谷口攀极顶,我欲东来抵九渊。 《颂李唐》 欲看诗人作楚狂,开元过...
    顿悟达观阅读 554评论 6 21
  • 每一个满心欢喜的现在,都有一个异常艰难的过去。我们每个人都活在精心设计的幻觉里,认为人可以无止境地发挥能力与追求进...
    虞七七阅读 221评论 0 8
  • 我是日记星球267号星宝宝,我正在参加日记星球第十一期21天蜕变之旅,这是我的第83篇原创日记。 时间真快,四月底...
    张小姐4134阅读 627评论 6 4