# 由爬楼梯问题引发的思考

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.

Method 1: 递归求解

class Solution(object):
def climbStairs(self, n):
if n == 1 or n <= 0:
return 1
return climbStairs(n-1) + climbStairs(n-2)

Method 2: 非递归求解

class Solution(object):
def climbStairs(self, n):
a = b = 1
for x in range(2, n + 1):
a, b = b, a + b
return b

Method 3：通项公式求解

class Solution(object):
def climbStairs(self, n):
sqrt5 = math.sqrt(5)
Phi = (1 + sqrt5) / 2
phi = (1 - sqrt5) / 2
return int((Phi ** (n + 1) - phi ** (n + 1)) / sqrt5)

Method 4: 杨辉三角叠加法

class Solution(object):
def c(self, m, n):
x = 1
for i in xrange(m-n+1, m+1):
x *= i
for i in xrange(2, n+1):
x /= i
return x
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
result = 0
for i in range(0, n/2+1):
result += self.c(n-i, i)
return result

# 来自小姐姐的鸡汤

/*
int total = 0;
for (int i = 0; i < doc['goals'].length; ++i)
{
total += doc['goals'][i];
}
*/