目录
- T9-斐波那契用迭代
- 跳台阶
- 动规 py2
- 变态跳台阶
- wo的初始迭代方法
- 数学--移位 py2
- 矩形覆盖
- 动规 Py2
T9-斐波那契用迭代
跳台阶
动规 py2
# -*- 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
变态跳台阶
wo的初始迭代方法
# -*- 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]
数学--移位 py2
调到第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)
矩形覆盖
动规 Py2
# -*- 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
原文地址:https://www.cnblogs.com/yxl-2018/p/12395396.html
时间: 2024-10-01 09:39:49