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

加起来和为目标值的组合

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
题解 文章被收录于专栏

算法题解

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 12:02
ssob上原来真有BOSS啊
硫蛋蛋:这种也是打工的,只不是是给写字楼房东打工
点赞 评论 收藏
分享
缒梦&独舞:这家公司是这样的,去年给我实习offer了,不过也是面着玩儿的,他周六还要去做公益志愿活动
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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