美文网首页
Leetcode 70. Climbing Stairs

Leetcode 70. Climbing Stairs

作者: persistent100 | 来源:发表于2017-07-01 11:39 被阅读0次

题目

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.

分析

很简单的动态规划,通过分析能够得到,F(n)=F(n-1)+F(n-2)。然后通过简单的数组迭代就能得到结果。当然也可以利用Fibonacci数列的简便

int climbStairs(int n) {
    if(n==0||n==1)return 1;
    int a[1000000];
    a[0]=1;
    a[1]=1;
    for(int i=2;i<=n;i++)
    {
        a[i]=a[i-1]+a[i-2];
    }
    return a[n];
}

相关文章

网友评论

      本文标题:Leetcode 70. Climbing Stairs

      本文链接:https://www.haomeiwen.com/subject/scbwcxtx.html