链接:
https://leetcode.com/problems/combination-sum-ii/
大意:
给定一个数组c(c中有重复的数字)和一个目标值t。要求使用c中的数字(每一个位置的数只能被使用一次)组合,组合的和为t。求所有这样的组合。另:c中所有元素以及t都是正数。例子:

思路:
和前一个题思路一样,采用回溯法。前一题:https://blog.youkuaiyun.com/smart_ferry/article/details/88789258
但与前一题有一些区别:
(1)本题中数组含有重复的数字,前一题数组不含重复的数字
(2)本题中在一个组合中,数组的元素只能被使用一次;前一题数组元素可以被取多次
同样有一个特殊情况,那就是 c.length == 0时直接返回空列表即可
对数组c排序,更好地利于dfs
解决每个数字只能取一次的方法:在dfs中,对于上次加入到list末尾的元素,下次dfs从该元素的下一个元素开始判断
解决最终res中不能含有相同的组合方法:若两个数num1和num2相同,假设num1排在num2前面一位,则对于选取num1并判断最终的组合是否满足题意之后(最终的组合为subSum>t或者subSum==t),下次不应该选取num2,而应该选下一个与num1不同的数
最终的优化的一点:若在选取组合中的第一个元素时,发现第一个元素若大于t,则可直接返回res;若第一个元素等于t,则res添加一个只含t的列表,再返回t;否则需要进行dfs
代码:
class Solution {
public List<List<Integer>> combinationSum2(int[] c, int t) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (c.length == 0)
return res;
Arrays.sort(c);
LinkedList<Integer> list = new LinkedList<>();
// 用于找到组合中的第一个数
for (int i = 0;i < c.length; i++) {
// 去重
if (i == 0 || c[i] != c[i - 1]) {
// 如果c[i] == t 则res直接加一个只含c[i]的列表 然后跳出循环即可 因为c[i]之后的数大于或等于t 而res中不能含有重复的组合
if (c[i] == t) {
res.add(Arrays.asList(c[i]));
break;
} else if (c[i] < t){
list.addLast(c[i]);
dfs(c, t, c[i], i, res, list);
list.removeLast();
} else {
break;
}
}
}
return res;
}
public void dfs(int[] c, int t, int subSum, int index, List<List<Integer>> res, LinkedList<Integer> list) {
// 每次从上一个元素位置index的下一个位置开始dfs
for (int i = index + 1; i < c.length; i++) {
// 去重
if (i == index + 1 || c[i] != c[i - 1]) {
if (subSum + c[i] < t) {
list.addLast(c[i]);
dfs(c, t, subSum + c[i], i, res, list);
list.removeLast();
} else if (subSum + c[i] == t) {
list.addLast(c[i]);
res.add(new ArrayList<>(list));
list.removeLast();
return ;
} else {
return ;
}
}
}
}
}
结果:

结论:
代码有冗余,待优化。
最佳:
class Solution {
public List<List<Integer>> combinationSum2(int[] c, int t) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (c.length == 0)
return res;
Arrays.sort(c);
LinkedList<Integer> list = new LinkedList<>();
dfs(c, t, 0, 0, res, list);
return res;
}
public void dfs(int[] c, int t, int subSum, int index, List<List<Integer>> res, LinkedList<Integer> list) {
for (int i = index; i < c.length; i++) {
if (i == index || c[i] != c[i - 1]) {
if (subSum + c[i] > t) {
return ;
} else if (subSum + c[i] == t) {
list.addLast(c[i]);
res.add(new ArrayList<>(list));
list.removeLast();
return ;
} else {
list.addLast(c[i]);
dfs(c, t, subSum + c[i], i + 1, res, list);
list.removeLast();
}
}
}
}
}
虽然看着还是有点冗余,但是可以在判断subSum+c[i]>=t的时候,可以减少一次函数调用的过程了(强行安慰自己,其实就是菜...)。最后附一个别人家的代码。链接:https://leetcode.com/problems/combination-sum-ii/discuss/16861/Java-solution-using-dfs-easy-understand
public List<List<Integer>> combinationSum2(int[] cand, int target) {
Arrays.sort(cand);
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> path = new ArrayList<Integer>();
dfs_com(cand, 0, target, path, res);
return res;
}
void dfs_com(int[] cand, int cur, int target, List<Integer> path, List<List<Integer>> res) {
if (target == 0) {
res.add(new ArrayList(path));
return ;
}
if (target < 0) return;
for (int i = cur; i < cand.length; i++){
if (i > cur && cand[i] == cand[i-1]) continue;
path.add(path.size(), cand[i]);
dfs_com(cand, i+1, target - cand[i], path, res);
path.remove(path.size()-1);
}
}
1680

被折叠的 条评论
为什么被折叠?



