题解 | #加起来和为目标值的组合(四)#

加起来和为目标值的组合(四)

http://www.nowcoder.com/practice/7a64b6a6cf2e4e88a0a73af0a967a82b

dfs, 由于数字可重复且顺序不同视为不同组合,则递归求解和为 target 的组合即可

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param nums int整型一维数组 
# @param target int整型 
# @return int整型
#
class Solution:
    def combination(self , nums: List[int], target: int) -> int:
        # write code here
        res = 0
        def dfs(t):
            nonlocal res
            if sum(t) > target:
                return
            if sum(t) == target:
                res += 1
                return
            for num in nums:
                dfs(t + [num])
        dfs([])
        return res
题解 文章被收录于专栏

算法题解

全部评论

相关推荐

06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务