Given a set of candidate numbers (candidates
) (without duplicates) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sums to target
.
The same repeated number may be chosen from candidates
unlimited number of times.
Note:
- All numbers (including
target
) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Java
自己的代码:(递归 + 回溯)
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> results = new LinkedList<>();
LinkedList<Integer> tempList = new LinkedList<>();
int tempSum = 0;
helper(results,tempList,tempSum,candidates,target,0);
return results;
}
public static void helper(List<List<Integer>> results ,
LinkedList<Integer> tempList,int tempSum, int[] candidates, int target,int candidate) {
if(tempSum == target) {
List<Integer> list= new LinkedList<>();
for(Integer i: tempList) {
list.add(i);
}
results.add(list);
return;
}else if(tempSum > target) { // 缺这个,就会递归栈溢出;
return;
}
for(int i=0;i<candidates.length;i++) {
if(candidates[i] >= candidate) { // 无此if判断,则会有重复的结果集
tempSum = tempSum + candidates[i];
tempList.addLast(candidates[i]);
helper(results,tempList,tempSum,candidates,target,candidates[i]);
tempSum = tempSum - candidates[i];
tempList.removeLast();
}
}
}
1)
Python:
方法一:
Runtime: 108 ms, faster than 30.64% of Python online submissions for Combination Sum.
Memory Usage: 10.8 MB, less than 35.71% of Python online submissions for Combination Sum.
class Solution(object):
def combinationSum(self, candidates, target):
res = []
candidates.sort() # 列表[]排序
self.dfs(candidates, target, 0, [], res)
return res
def dfs(self, nums, target, index, path, res):
if target < 0:
return # backtracking
if target == 0:
res.append(path)
return
for i in xrange(index, len(nums)):
self.dfs(nums, target-nums[i], i, path+[nums[i]], res)
1)xrange是Python2的函数,生成列表的函数(含头不含尾)
2) we have to write path + [nums[i]] instead of path.append(nums[i]),知道栈帧内存分配了,就很好理解
改进:
Runtime: 44 ms, faster than 99.58% of Python online submissions for Combination Sum.
Memory Usage: 10.8 MB, less than 61.11% of Python online submissions for Combination Sum.
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res=[]
candidates.sort()
self.dfs(candidates,target,0,[],res)
return res
def dfs(self,nums,target,index,path,res):
if target<0:
return
if target==0:
res.append(path)
return
i=index
while(i<len(nums) and nums[i]<=target):
self.dfs(nums,target-nums[i],i,path+[nums[i]],res)
i+=1
return res
1) 这里nums[i]<=target如果没有这个条件判断,那么还是可以使用for循环,循环中要有条件判断就用while
slightly more concise,学习下Python的语法
class Solution:
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = []
candidates.sort()
def dfs(target, index, path):
if target < 0:
return # backtracking
if target == 0:
res.append(path)
return
for i in range(index, len(candidates)):
dfs(target-candidates[i], i, path+[candidates[i]])
dfs(target, 0, [])
return res