LeetCode | Climbing Stairs

Climbing Stairs

 Total Accepted: 55005 Total Submissions: 160176My Submissions

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?

Show Tags
Have you met this question in a real interview? 
Yes
 
No

Discuss

思路:自己找规律,F(1)=1,F(n)=F(n-1)+F(n-2).

实现代码:

<span style="font-size:12px;">class Solution {
public:
    int climbStairs(int n) {
        int a[100000];
        memset(a,0,sizeof(a));
        a[1]=1;
        a[2]=2;
        for(int i=3;i<=n;i++)
        {
            a[i]=a[i-1]+a[i-2];
        }
        return a[n];
    }
};</span>

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/Tobyuyu/p/4965321.html