LintCode_111 爬楼梯

题目

假设你正在爬楼梯,需要n步你才能到达顶部。但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部?

比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法

返回 3

1  2  3  5  8  13...

step[2] = step[0] + step[1];

C++代码

int climbStairs(int n) {
   // write your code here
   if(n == 0) return 1;
   if(n <= 2) return n;
   int step_1,step_2,step_3;
   step_1 = 1;
   step_2 = 2;
   for(int i = 2; i < n; ++i)
   {
       step_3 = step_1 + step_2;
       step_1 = step_2;
       step_2 = step_3;
   }
   return step_3;
}

  

原文链接: https://www.cnblogs.com/Smallhui/p/5450716.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    LintCode_111 爬楼梯

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/232775

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午3:36
下一篇 2023年2月13日 下午3:37

相关推荐