Combination Sum Two
Description:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5]
and target 8
,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
] Thoughts:
这个问题和前一个问题Combination Sum很像,区别在于它允许给定的nums中有重复数字,但是不允许对nums中的数字进行重复使用.同时还不能够出现重复的解
解决不允许对nums中的数字进行重复使用,我们只需要将下一次回溯的开始点设置为当前回溯点的下一个点即可。不能出现重复解,这个问题我们可以通过在添加解的时候判断当前的解是否已经在已有的解中来解决(另外一种解决方式是
通过添加限制条件来减少当前解的搜索空间。因为我们不允许出现重复解,但是nums中又有重复数字,因此我们只需要保证搜索路径跳过重复的数字即可。) 以下是java解法一:
package middle; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class CombinationSum { public List<List<Integer>> combinationSum(int[] candidates, int target){
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
backtrack(list, new ArrayList(),candidates, target, 0);
/* System.out.println(list);*/
return list;
} private void backtrack(List<List<Integer>> list, ArrayList arrayList,
int[] candidates, int target, int start) {
// TODO Auto-generated method stub
if(target < 0){
return;
}else if(target == 0){
//we should new an ArrayList, because the arrayList will change later,
//if we do not copy it's value, list will add []
list.add(new ArrayList<Integer>(arrayList));
}else{
for(int i = start;i<candidates.length;i++){
arrayList.add(candidates[i]);
backtrack(list, arrayList, candidates, target-candidates[i], i);
arrayList.remove(arrayList.size() - 1);
}
}
} public static void main(String[] args){
CombinationSum sum = new CombinationSum();
int[] candidates = new int[]{2, 3,5 ,7};
sum.combinationSum(candidates, 7);
}
}
这个解法采用的是通过在添加解的时候判断当前的解是否已经在已有的解中来解决重复问题
以下是java解法二:
package middle; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class CombinationSumTwo { public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
backTrack2(list, new ArrayList(), candidates, target, 0);
return list;
} private void backTrack(List<List<Integer>> list, ArrayList arrayList,
int[] candidates, int target, int start) {
// TODO Auto-generated method stub if(target < 0){
return;
}else if (target == 0){
if(list.contains(arrayList) == false){
list.add(new ArrayList(arrayList));
}
}else{
for(int i = start; i < candidates.length;i++){
arrayList.add(candidates[i]);
backTrack(list, arrayList, candidates, target-candidates[i], i+1);
arrayList.remove(arrayList.size() - 1);
}
}
} private void backTrack2(List<List<Integer>> list, ArrayList arrayList, int[] candidates, int target, int start){
if(target < 0){
return;
}else if(target == 0){
//we should new an ArrayList, because the arrayList will change later,
//if we do not copy it's value, list will add []
list.add(new ArrayList<Integer>(arrayList));
}else{
for(int i = start;i<candidates.length;i++){
if(i > start && candidates[i] == candidates[i - 1]) continue;
arrayList.add(candidates[i]);
backTrack2(list, arrayList, candidates, target-candidates[i], i+1);
arrayList.remove(arrayList.size() - 1);
}
}
} public static void main(String[] args){
CombinationSumTwo c = new CombinationSumTwo();
int[] candidates = new int[]{10, 1, 2, 7, 6, 1, 5};
List<List<Integer>> l = c.combinationSum2(candidates, 8);
System.out.println(l);
} }
这个解法采用了减少搜索空间的方式来去除重复的解。
Combination Sum Two的更多相关文章
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【leetcode】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
随机推荐
- 【一天一道LeetCode】#92. Reverse Linked List II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...
- 实战项目:通讯录 UI—第十一天
1.推出视图的两种方式: 1.通过导航控制器push到下一个界面,使用pop返回到上一个界面 2.通过模态的形式推出视图,不需要依赖于导航控制器,通过使用present到下一个界面,通过dismi ...
- 调用sed命令的三种方式
调用sed命令的三种方式 调用sed有三种方式,一种为Shell命令行方式,另外两种是将sed命令写入脚本文件,然后执行该脚本文件. 三种方式的命令格式归纳如下: 一.在Shell命令行输入命令调用s ...
- 一键安装LAMP
一键安装LAMP LAMP是Linux,Apache,MySQL和PHP合起来的简称,用于开发网站.对于初学者而言,没有什么比一键部署一个LAMP开发环境更省心的了,到下面的网址下载BitNami: ...
- 纯命令提交代码到git仓库(教你怎么装逼)
如果不喜欢用命令的请点链接:http://blog.csdn.net/xiangzhihong8/article/details/50715427 我这里用纯命令,主要是因为这两天不知道什么原因,ba ...
- Unity3D学习笔记(五)C#与JavaScript组件访问的比较
由于之前用JavaScript用的比较多,因此总是想用以前的方法来访问组件,却屡遭失败,经过查阅资料发现,二者存在较大的不同. 下面以调用3D Text组件HurtValue为例,来比较二者的不同 J ...
- ITU-T G.1081 IPTV性能监测点 (Performance monitoring points for IPTV)
ITU-T 建议书 G.1081 IPTV性能监测点 Performance monitoring points for IPTV Summary Successful deployment of I ...
- “《编程珠玑》(第2版)第2章”:B题(向量旋转)
B题是这样子的: 将一个n元一维向量向左旋转(即循环移位)i个位置.例如,当n=8且i=3时,向量abcdefgh旋转为defghabc.简单的代码使用一个n元的中间向量在n步内完成该工作.你能否仅使 ...
- JAVA中重写equals()方法的同时要重写hashcode()方法
object对象中的 public boolean equals(Object obj),对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true:注意:当此方法 ...
- 巧用FineReport搭建成本管控监测系统
一.应用背景 企业在近几年快速发展,规模也越来越大,而信息传递与反馈手段却仍然比较落后,随着信息技术的不断发展,人们开始通过尝试利用技术手段改善这种环境.企业的项目不断增多,而作为高层管理者,通过层级 ...