您的位置:首页 > 大数据 > 人工智能

Climbing Stairs

2015-08-22 09:53 357 查看
题目: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步的走法。代码如下:

class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
count = 1
if n == 1:
return 1

num2 = n // 2
i = 1
while i <= num2:
tamp = n - i
dividend = tamp
divisor = 1
#计算被除数
j = 1
while j < i:
dividend *= (tamp-j)
j += 1
#计算除数
j = 1
while j <= i:
divisor *= j
j += 1

count += (dividend / divisor)
i += 1

return count
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: