70. 爬楼梯

题目

代码

class Solution {
public:
    int climbStairs(int n) {
        
        if(n==1)
            return 1;
        int step=0;
        int step1=1,step2=1;
        for(int i=1;i<n;i++)
        {
            step=step1+step2;
            step1=step2;
            step2=step;
        }
        return step;
    }
};

思路

经典动态规划题,每一层楼梯跟前面两层楼梯的步数相关,即可得出状态转移公式。

https://github.com/li-zheng-hao
原文地址:https://www.cnblogs.com/lizhenghao126/p/11053621.html