Climbing Stairs

题目: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?

思路:我的解题思想是排列组合的思想,看看路面有多少个2步的走法。代码如下:

 1 class Solution(object):
 2     def climbStairs(self, n):
 3         """
 4         :type n: int
 5         :rtype: int
 6         """
 7         count = 1
 8         if n == 1:
 9             return 1
10         
11         num2 = n // 2
12         i = 1
13         while i <= num2:
14             tamp = n - i
15             dividend = tamp
16             divisor = 1
17             #计算被除数
18             j = 1
19             while j < i:
20                 dividend *= (tamp-j)
21                 j += 1
22             #计算除数
23             j = 1
24             while j <= i:
25                 divisor *= j
26                 j += 1
27                 
28             count += (dividend / divisor)
29             i += 1
30             
31         return count
32             

 

posted @ 2015-08-22 09:53  双音节的秋  阅读(124)  评论(0编辑  收藏  举报