Monday, November 25, 2013

LeetCode Problem : Climbing Stairs

Problem


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?

Code

int climbStairs(int n) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    if(n < 3)
        return n;
    vector<int> M(n);
    M[0] = 1;
    M[1] = 2;
    for(int i = 2; i < n; ++i)
        M[i] = M[i - 1] + M[i - 2];
    return M[n - 1];
}

No comments:

Post a Comment