70.爬楼梯

来源:互联网 发布:python学习手册 mobi 编辑:程序博客网 时间:2024/05/16 07:36

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?

Note: Given n will be a positive integer.

测试代码(c++):

class Solution {public:    int climbStairs(int n) {        if(n<=2)            return n;        vector<int> dp(n,0);        dp[0] = 1;        dp[1] = 2;        for(int i=2;i<n;i++)        {            dp[i] = dp[i-2]+dp[i-1];        }        return dp[n-1];    }};

性能:

这里写图片描述