You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
666 B

8 months ago
'''
假设你正在爬楼梯需要 n 阶你才能到达楼顶每次你可以爬 1 2 个台阶你有多少种不同的方法可以爬到楼顶呢
注意给定 n 是一个正整数
示例 1输入 2 输出 2 解释1 +1 / 2
示例 2输入 3 输出 3 解释1 +1 +1 / 1 +2 / 2 +1
'''
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 1:
return 1
pre,ppre = 1,1
for i in xrange(2,n+1):
tmp = pre
pre = pre + ppre
ppre = tmp
return pre