[leetcode] Climbing Stairs

Climbing Stairs

状态:

step[1]=1;
step[2]=2;
step[i]=step[i-1]+step[i-2];

class Solution {
public:
    int climbStairs(int n) {
        int *step=new int[n+1];//动态创建数组
        step[1]=1;
        step[2]=2;
        int i=3;
        while (i<=n) {
            step[i]=step[i-1]+step[i-2];
            ++i;
        }
        int res=step[n];
        delete [] step;//释放内存
        return res;
    }
};


参考:climbing stairs


你可能感兴趣的:([leetcode] Climbing Stairs)