题目描述:给定一个无重复元素的数组 candidates 和一个目标数 target ,找出candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。
说明:所有数字(包括 target)都是正整数。解集不能包含重复的组合。
示例 1:
输入: candidates = [2,3,6,7], target = 7,
所求解集为:[[7],[2,2,3]]
解题思路:我使用的是笨办法,找出该数组满足target的全排列,再插入解集前,判断该排列是否在解集中存在,如果存在,不插入。全排列的求解查看全排列(Python3)
代码如下( ̄▽ ̄):
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
self.re = []
self.target = target
self.candidates = candidates
self.sub_combinationSum(target,[])
return self.re
def sub_combinationSum(self,target,temp):
if target<0:
return
if target==0:
temp.sort()
if temp not in self.re:
self.re.append(temp)
else:
for i in self.candidates:
self.sub_combinationSum(target-i,temp+[i])
时间与空间复杂度:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/probl...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。