首页 > 其他 > 详细

爬楼梯

时间:2017-03-08 12:25:25      阅读:185      评论:0      收藏:0      [点我收藏+]

爬楼梯

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

样例

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

返回 3

class Solution {

public:    

/**      * @param n: An integer   

   * @return: An integer      */

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;  

   }

};

爬楼梯

原文:http://www.cnblogs.com/zhangying6/p/6517276.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!