Leetcode 70. Climbing Stairs

来源:互联网 发布:淘宝女包2016新款上市 编辑:程序博客网 时间:2024/06/08 02:52

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?

Note: Given n will be a positive integer.

class Solution {public:    int climbStairs(int n) {        if (n == 0) return 1;        array<int, 2> a;        a[0] = 1;        a[1] = 2;        if (n <= 2) return a[n - 1];        size_t id = 1;        for (size_t i = 3; i <= n; ++i) {            id = 1 - id;            a[id] = a[0] + a[1];        }        return a[id];    }};
原创粉丝点击