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); } }}