题目3:爬楼梯

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

样例

比如n=3,1+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

        int one = 0;

      int two = 1;

      while(n>0)  {

          two=one+two;

          one=two-one;

          n--;

      }

      return two;

    }

};

posted @ 2017-04-22 21:49  GodZe  阅读(219)  评论(0编辑  收藏  举报