求多少种一般是动态规划 要具体写出各种的情况一般是dfs
本题类似于换硬币
class Solution:
def combinationSum4(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
dp=[0]*(target+1)
dp[0]=1
for i in range(1,target+1):
for n in nums:
if n<=i: dp[i]+=dp[i-n]
return dp[target]
本文介绍了一种使用动态规划解决组合数问题的方法,通过类比换硬币问题,详细阐述了如何通过递推公式计算从给定数字集合中组成特定目标值的方法数。代码示例清晰展示了动态规划数组的初始化和更新过程。

3212

被折叠的 条评论
为什么被折叠?



