剑指Offer9:变态跳台阶

思路:

因为n级台阶,第一步有n种跳法:跳1级、跳2级、到跳n级
跳1级,剩下n-1级,则剩下跳法是f(n-1)
跳2级,剩下n-2级,则剩下跳法是f(n-2)
所以f(n)=f(n-1)+f(n-2)+...+f(1)+f(0)
因为f(n-1)=f(n-2)+f(n-3)+...+f(1)+f(0)

所以f(n)=2*f(n-1) 其中f(1)=1,n>=2

# -*- coding:utf-8 -*-
class Solution:
    def jumpFloorII(self, number):
        # write code here
        if number<=0:
            return 0
        else:
            return pow(2,number-1)

猜你喜欢

转载自blog.csdn.net/weixin_43160613/article/details/84562165