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?

class Solution {
public:
       // int p;
    int climbStairs(int n) {
        /*
        //int *p=new int;
        //int p;
        if(n==1)
        //*p=1;
        p=1;
        else if(n==2)
        //*p=2;
        p=2;
        else
        {
          //  *p =climbStairs(n-2)+climbStairs(n-1);
           // *p += climbStairs(n-2);
           p =climbStairs(n-2)+climbStairs(n-1);
        }
       // return *p;
       return p;
       */

        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }
        int f[n + 1];
        f[1] = 1;
        f[2] = 2;
        for (int i = 3; i <= n; ++i) {
            f[i] = f[i - 1] + f[i - 2];
        }
        return f[n];

    }
};
posted @ 2015-11-28 14:14  djiankuo  阅读(142)  评论(0编辑  收藏  举报