3月3日(6) Climbing Stairs

原题 Climbing Stairs

求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了。

class Solution {
public:
    int climbStairs(int n) {
        if (n==0) return 0;
        
        int n1 = 0;
        int n2 = 1;
        
        for (int i=0; i<n; ++i)
        {
            int t = n2;
            n2 = n1 + n2;
            n1 = t; 
        }
        return n2;
    }
};

还是觉得没有ACM难度,继续做吧。

posted @ 2014-03-03 20:19  miuc  阅读(135)  评论(0编辑  收藏  举报