Leetcode 70 Climbing Stairs

2018-01-12 15:00:11 浏览数 (1)

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?

每次爬一到两步,到终点有多少种办法?

简单DP,往后1步2步转移。

代码语言:javascript复制
class Solution {
public:
    int climbStairs(int n) {
        vector<int> dp(n 1,0);
        dp[0]=1;
        for(int i=0;i<=n;i  )
        {
            if(i 1<=n) dp[i 1] =dp[i];
            if(i 2<=n) dp[i 2] =dp[i];
        }
        return dp[n];
    }
};

0 人点赞