题解 | #跳台阶#
跳台阶
http://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4
# -*- coding:utf-8 -*-
class Solution:
def jumpFloor(self, number):
# write code here
#典型的动态规划/递归问题,设第i次有dp[i]种解法,则dp[i]=dp[i-1]+dp[i-2]
#由于空间复杂度为O(1),用三个变量保存这三个状态即可
dp1 = 1
dp2 = 2
dp3 = 3
if number ==1:
return dp1
if number ==2:
return dp2
for i in range(2,number):
dp3 = dp2+dp1
#状态前移
dp1 = dp2
dp2 = dp3
return dp3