LeetCode 70. Climbing Stairs

来源:互联网 发布:linux编辑器 编辑:程序博客网 时间:2024/05/02 02:25

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?

 斐波那契数列思想。

class Solution {public:    int climbStairs(int n) {        vector<int> v;        v.push_back(0);        v.push_back(1);        v.push_back(2);        if(n <= 2)            return v[n];        int i;        for(i = 3; i <= n; i ++){            v.push_back(v[i - 1] + v[i - 2]);        }        return v[n];    }};


0 0