Skip to content

377. 组合总数4 ​

  • 题号:377
  • 来源:LeetCode
  • 难度:中等
  • 标签:dp
  • 语言:Python
  • 解法:1 个
  • 作者:lmliheng
  • 最近更新:2026-09-29

Python · O(n*target) 时间 · O(target) 空间 · 更新于 2026-09-29

完全背包求排列数,外层枚举容量

python
"""
@difficulty medium
@tags dp
@time O(n*target)
@space O(target)
@note 完全背包求排列数,外层枚举容量
组合总和 Ⅳ
lc 377
动态规划题
"""
class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        dp=[0]*(target+1)
        dp[0]=1
        for i in range(1,target+1):
            for num in nums:
                if i>= num:
                    dp[i]+=dp[i-num]
        return dp[target]

源码:python/leetcode/组合总数4.py


在 GitHub 上查看题目所在目录:lmliheng/algorithm