变态跳台阶 牛客网 剑指Offer
- 题目描述
- 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
class Solution:
def jumpFloorII(self, number):
return 2**(number - 1)
def jumpFloorII2(self, number):
return pow(2,number - 1)
def jumpFloorI3(self, number):
return self.mypow(2,number - 1)
def mypow(self,x,n):
if n == 0:
return 1
if n%2 == 0:
return self.mypow(x*x,n/2)
else:
return self.mypow(x*x,n/2)*x
#run:27ms memory:5680k
def jumpFloorII4(self, number):
if number <= 0:
return 0
if number == 1:
return 1
if number == 2:
return 2
i = 3
lt = []
lt.append(1)
lt.append(2)
while i<=number:
ret = 0
for j in range(len(lt)):
ret = ret + lt[j]
ret +=1
lt.append(ret)
i +=1
return lt[number-1]