Climbing Stairs

来源:互联网 发布:大数据300a基金001420 编辑:程序博客网 时间:2024/05/29 10:42

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?


Solution:

class Solution {public:    int climbStairs(int n) {        if(n < 1) return 0;        int c1 = 1, c2 = 2, c;        if(n == 1) return c1;        if(n == 2) return c2;        for(int i = 3; i <= n; ++i)        {            c = c1 + c2;            c1 = c2;            c2 = c;        }        return c;    }};


0 0