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

加起来和为目标值的组合

http://www.nowcoder.com/practice/172e6420abf84c11840ed6b36a48f8cd

递归,可重复选取一个元素,由于不能有重复的数组出现,则当符合条件时,进行排序判断是否已在结果数组中

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

算法题解

全部评论

相关推荐

稽鱼:简历好丑啊,换个模板,别用红色字体
点赞 评论 收藏
分享
迷茫的大四🐶:干脆大厂搞个收费培训得了,这样就人均大厂了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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