题解 | #加起来和为目标值的组合#
加起来和为目标值的组合
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
题解 文章被收录于专栏
算法题解