LeetCode -- Climbing Stairs

Source: Internet
Author: User

LeetCode -- Climbing Stairs
Description:


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?


There are n-level stairs, and the number of different upper steps can be obtained. Only one or two stairs can be obtained at a time.


The number of situations where the stairs are to be taken up, that is, the sum of the situations where the stairs are to be taken down, that is, the first, second, and second, that is:
ClimbStairs (n) = ClimbStairs (n-1) + ClimbStairs (n-2 ).


The recurrence of this question is completely consistent with the Fibonacci series. Therefore, you can use the Fast Algorithm of the Fibonacci series to solve this problem, thus improving the efficiency. The only difference is that there is only one stair.


For the Fast Algorithm of Fibonacci, refer to the author's article:
Http://blog.csdn.net/lan_liang/article/details/40825863




Implementation Code of this question:


public class Solution {    public int ClimbStairs(int n)     {    return Go(n + 1);    }    private int Go(int n)    {    if(n == 1 || n == 2)    {    return 1;    }        if(n%2 == 0)    {    var k = n/2;    return Go(k) * (2*Go(k+1) - Go(k));    }    else    {    var k = (n-1)/2;    return Go(k+1) * Go(k+1) + Go(k) * Go(k);    }    }}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.