解法一,用递归
class Solution:
def jumpFloor(self, number):
# write code here
flo = []
for i in range(number):
if i ==0:
flo.append(1)
elif i ==1:
flo.append(2)
else:
flo.append(flo[i-1]+flo[i-2])
return flo.pop()
运行时间:25ms
占用内存:5860k
解法二,用迭代
# -*- coding:utf-8 -*-
class Solution:
def jumpFloor(self, number):
# write code here
a,b=1,2
if number==1:
return a
if number == 2:
return b
for i in range(number-1):
a,b = b,a+b
return a
运行时间:33ms
占用内存:5728k