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.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1a2 ≤ … ≤ ak).
  • 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]

解题思路:

修改上题代码,将DFS宽度设置成2即可,注意使用Set,防止重复,JAVA实现如下:

public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Set<List<Integer>> list = new HashSet<List<Integer>>();
Arrays.sort(candidates);
dfs(list, candidates, 0, target, 0);
return new ArrayList<List<Integer>>(list);
}
static List<Integer> list2 = new ArrayList<Integer>();
static void dfs(Set<List<Integer>> list, int[] array, int result,int target, int depth) {
if (result == target) {
list.add(new ArrayList<Integer>(list2));
return;
}
else if (depth >= array.length || result > target)
return;
for (int i = 0; i <= 1; i++) {
for (int j = 0; j < i; j++)
list2.add(array[depth]);
dfs(list, array, result + array[depth] * i, target, depth+1);
for (int j = 0; j < i; j++)
list2.remove(list2.size() - 1);
}
}

结果453 ms,效率略低,因此换掉Set,用一个变量计算每次DFS的宽度,JAVA实现如下:

public List<List<Integer>> combinationSum2(int[] candidates, int target) {
ArrayList<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
dfs(list, candidates, 0, target, 0);
return list;
}
static List<Integer> list2 = new ArrayList<Integer>();
static void dfs(ArrayList<List<Integer>> list, int[] array, int result,int target, int depth) {
if (result == target) {
list.add(new ArrayList<Integer>(list2));
return;
}
else if (depth >= array.length || result > target)
return;
int step=1;
while(depth<array.length-1&&array[depth]==array[depth+1]){
depth++;
step++;
}
for (int i = 0; i <= step; i++) {
for (int j = 0; j < i; j++)
list2.add(array[depth]);
dfs(list, array, result + array[depth] * i, target, depth+1);
for (int j = 0; j < i; j++)
list2.remove(list2.size() - 1);
}
}

Java for LeetCode 040 Combination Sum II的更多相关文章

  1. LeetCode 040 Combination Sum II

    题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find al ...

  2. 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 ...

  3. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  4. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  6. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  7. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  8. leetcode 40 Combination Sum II --- java

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  9. 【LeetCode】040. Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

随机推荐

  1. bootstrap table简洁扁平的表格

    使用方法 1.在html页面的head标签中引入Bootstrap库(假如你的项目还没使用)和bootstrap-table.css. <link rel="stylesheet&qu ...

  2. javaScript基础练习题-下拉框制作(CSS)

    http://www.imooc.com/video/155 慕课网的视频,直接上代码 <!DOCTYPE hmtl> <html> <head> <meta ...

  3. BZOJ1816 [Cqoi2010]扑克牌

    Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的 牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1 ...

  4. leach和leach-c协议仿真

    http://blog.csdn.net/codingkid/article/details/7215216 1.复制leach_test为leach-c_test,修改里面的文件夹和输出文件名.并且 ...

  5. Java_观察者模式(Observable和Observer)

    http://blog.csdn.net/tianjf0514/article/details/7475164/ 一.观察者模式介绍 在Java中通过Observable类和Observer接口实现了 ...

  6. Request.InputStream 接收Post Data

    今天 用 Request.Form /Request.Params 等怎么也获取不到客户发过来的Post 数据 后来试着用了 Request.InputStream 竟然可以 using (Syste ...

  7. localdb链接字符串

    <add name="GitCandyContext" connectionString="Data Source=(localdb)\v11.0;Integrat ...

  8. 通过开源程序同时解决DNS劫持和DNS污染的问题

    我们知道,某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址.常用的手段有:DNS劫持和DNS污染.关于DNS劫持和DNS污染的区别,请查找 ...

  9. centOS 下 VSFTP的安装和设置

    http://blog.csdn.net/swiftshow/article/details/7367609 一.FTP的安装 1.检测是否安装了FTP :[root@localhost ~]# rp ...

  10. update操作多张表

    sql 语句多张表UPDATE用法一.当用一个表中的数据来更新另一个表中的数据,T-SQL提供多种写法(下面列出了二种),但建议用第一种写法,虽然传统,但结构清晰.飞.飞Asp技术乐园并且要注意,当用 ...