518. 零钱兑换 II 代码简洁版

简洁容易理解

class Solution(object):
    def change(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int
        """
        
        dp =[[0]*(amount+1) for _ in range(len(coins)+1)]
        # 初始化角落条件[0][0]
        dp[0][0] = 1
        
        # 初始化边界条件1
        for i in range(1, len(coins)+1):
            dp[i][0] = 1
        
        # 初始化边界条件2
        for j in range(1, amount+1):
            dp[0][j] = 0
        
        # 递推公式
        for i in range(1, len(coins)+1):
            for j in range(1, amount+1):
                c = coins[i-1]
                if j >=c:
                    dp[i][j] = dp[i-1][j]+dp[i][j-c]
                else:
                    dp[i][j] = dp[i-1][j]
        return dp[-1][-1]   
    原文作者:cptn3m0
    原文地址: https://www.jianshu.com/p/6f78212fa62b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞