标签:page href 优化 迭代 移位 question ice self HERE
# -*- coding:utf-8 -*-
class Solution:
def jumpFloor(self, number):
# write code here
if number<0:
return -1
if number <=2:
return number
a,b=1,2
res = 0
for i in range(3,number+1):
res = a+b
a,b = b,res
return res
# -*- coding:utf-8 -*-
class Solution:
def jumpFloorII(self, number):
# write code here
if number<0:
return -1
res = [0,1]
for i in range(2,number+1):
res.append(sum(res)+1)
return res[number]
调到第n级台阶,前面的(n-1)级有跳与不跳两种选择吗,故答案为2^(n-1),并且用移位运算代替乘法运算来优化。
# -*- coding:utf-8 -*-
class Solution:
def jumpFloorII(self, number):
# write code here
if number<=0:
return -1
res = 1
return res<<(number-1)
# -*- coding:utf-8 -*-
class Solution:
def rectCover(self, number):
# write code here
if number<0:
return -1
if number<=2:
return number
a,b=1,2
res=0
for i in range(3,number+1):
res = a+b
a ,b = b,res
return res
标签:page href 优化 迭代 移位 question ice self HERE
原文地址:https://www.cnblogs.com/yxl-2018/p/12395396.html