Leetcodeブラッシングレコード-70。階段を登る

ここに画像の説明を挿入
ボトムアップの動的プログラミングを使用した簡単な質問

状態伝達方程式:f(n)= f(n-1)+ f(n-2)、n> = 3

class Solution:
    def climbStairs(self, n: int) -> int:
        if n == 1:
            return 1
        elif n == 2:
            return 2
        l_2 = 1#
        l_1 = 2
        #start i = 3
        for i in range(n-2):
            res = l_2 + l_1
            l_2 = l_1#
            l_1 = res
        return res
43件の元の記事を公開 14 件を賞賛・2 万回以上の閲覧

おすすめ

転載: blog.csdn.net/weixin_41545780/article/details/105051410