"Sword Finger Offer": Abnormal Jumping

"Sword Finger Offer": Abnormal Jumping

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Question :
    A frog can jump up to one level or two at a time... it can also jump up to n levels. Find the total number of jumping methods the frog jumps on an n-level step.
  • Example :
示例 1 :
输入:3
返回值:4
  • Code 1:
# -*- coding:utf-8 -*-
class Solution:
    def jumpFloorII(self, number):
        result = [1]
        if number == 1:
            return 1
        for i in range(number):
            result.append(sum(result[:]))
        return result[-1]
  • 算法说明:
    得到递推公式为:
    f ( n ) = f ( n − 1 ) + f ( n − 2 ) + f ( n − 3 ) + . . . + f ( n − ( n − 1 ) ) + f ( n − n ) f\left( n \right){\rm{ }} = {\rm{ }}f\left( {n - 1} \right){\rm{ }} + {\rm{ }}f\left( {n - 2} \right){\rm{ }} + {\rm{ }}f\left( {n - 3} \right){\rm{ }} + {\rm{ }}...{\rm{ }} + {\rm{ }}f\left( {n - \left( {n - 1} \right)} \right){\rm{ }} + {\rm{ }}f\left( {n - n} \right) f(n)=f(n1)+f(n2)+f(n3)+...+f(n(n1))+f(nn ) Direct algorithm implementation is sufficient.

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/115162228