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. ACM算法总结及刷题参考

    参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,p ...

  2. Windows Management Instrumentation WMI Security Technology Learning

    目录 . 引言 . WMI(Windows Management Instrumentation)简介 . 基于WMI的攻击向量 . WMI编程示例 0. 引言 在进行服务器主机的入侵检测.安全攻防的 ...

  3. 分享一段Java搞笑的代码注释

    今天在群里看到有人分享了一段搞笑的注释代码,觉得挺好玩的,在这里收藏一下 // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // ...

  4. vs配置

    每次遇到vs配置都要让我头疼一段时间,对于某些不太清楚,有时自己试着配置,能运行起来就行,下次又忘了咋陪的了,其中配置的东西真心多. 1.输出目录这样配置../../Bin/Server/ 这个路径是 ...

  5. OOA/OOD/OOP(了解)

    Object-Oriented Analysis:面向对象分析方法 是在一个系统的开发过程中进行了系统业务调查以后,按照面向对象的思想来分析问题.OOA与结构化分析有较大的区别.OOA所强调的是在系统 ...

  6. serialVersionUID要注意以下几点:

    今天在使用eclipse开发的时候,遇到一个warning,看到warning我总觉得不爽,使用自动修复后,发现eclipse在代码中加入了“private static final long ser ...

  7. rsync+inotify 实现服务器之间目录文件实时同步(转)

    软件简介: 1.rsync 与传统的 cp. tar 备份方式相比,rsync 具有安全性高.备份迅速.支持增量备份等优点,通过 rsync 可 以解决对实时性要求不高的数据备份需求,例如定期的备份文 ...

  8. alipay iOS SDK

    我也是醉了,进支付宝主页找都找不到,好不容易找到赶紧记下来:https://b.alipay.com/order/productDetail.htm?productId=201308060460965 ...

  9. Entity Framework简介

    前言 很久很久之前就想来好好的学习一下EF,一直也是各种理由导致一直也没有好好的开始,现在下定决心了,不管怎样一定要把这先走一遍,并且把自己学到的相关EF的知识进行记录,以备后用,也望广大博友们一起来 ...

  10. Unity3D Optimizing Graphics Performance for iOS

    原地址:http://blog.sina.com.cn/s/blog_72b936d801013ptr.html icense Comparisons http://unity3d.com/unity ...