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

leetcode之Climbing Stairs

2015-10-22 02:37 441 查看
这题其实跟斐波那契数列很像啦,都是num(n) = num(n - 1) + num(n -2),不过python并没有对尾递归做出优化,所以还是用的for循环来写的。代码如下;
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 1
if n == 2:
return 2
list1 = [1, 2]
for i in range(2, n):
list1.append(list1[i - 1] + list1[i - 2])
return list1[n - 1]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息