[leetcode]Climbing Stairs

Posted on 2013-07-25 19:15  冰天雪域  阅读(139)  评论(0编辑  收藏  举报
class Solution {
public:
    int climbStairs(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(n <= 2) return n;
        
        vector<int> f(n+1, 0);
        f[1] = 1;
        f[2] = 2;
        
        for(int i = 3; i <= n; i++){
            f[i] = f[i-1]+f[i-2];
        }
        
        return f[n];
        
    }
};


Copyright © 2024 冰天雪域
Powered by .NET 9.0 on Kubernetes