Climbing Stairs - LeetCode

Climbing Stairs - LeetCode

题目
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?


这篇的题目,包括接下来的几篇都是考试前为了练习而做的题目,难度都不是很大。
这道题目就是一个斐波那契数列,走到第k级台阶的种数=走到第k-1级台阶的种数+走到第k-2级台阶的种数

class Solution {
public:
    int climbStairs(int n) {
        if (n <= 1) return 1;
        int a, b, t;
        a = 1;
        b = 2;
        for (int i = 3; i <= n; i++) {
            t = a+b;
            a = b;
            b = t;
        }
        return b;
    }
};

猜你喜欢

转载自blog.csdn.net/sgafpzys/article/details/79116542
今日推荐