Combination Sum II

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, a1 ≤ a2 ≤ … ≤ 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]

SOLUTION 1:

注意,这里从 i = index开始
每次只取第一个,例如 123334,到了333这里,我们第一次只取第1个3,因为我们选任何一个3是对组合来说是一个解。所以只第一次取就好了。

 public List<List<Integer>> combinationSum2(int[] num, int target) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (num == null || num.length == 0) {
return ret;
} Arrays.sort(num); dfs(num, target, new ArrayList<Integer>(), ret, 0);
return ret;
} public void dfs1(int[] num, int target, ArrayList<Integer> path, List<List<Integer>> ret, int index) {
if (target == 0) {
ret.add(new ArrayList<Integer>(path));
return;
} if (target < 0) {
return;
} // 注意,这里从 i = index开始
// 每次只取第一个,例如 123334,到了333这里,我们第一次只取第1个3,因为我们选任何一个3是对组合来说是一个解。所以只
// 第一次取就好了。
int pre = -1;
for (int i = index; i < num.length; i++) {
int n = num[i];
if (n == pre) {
continue;
}
pre = n;
path.add(n);
dfs(num, target - n, path, ret, i + 1);
path.remove(path.size() - 1);
}
}

SOLUTION 2:

不使用pre来判断也可以,只要判断当前值是不是与上一个值相同,如果相同不取。我们只考虑i = index即可,因为这么多相同的值也只需要取一个

 public void dfs(int[] num, int target, ArrayList<Integer> path, List<List<Integer>> ret, int index) {
if (target == 0) {
ret.add(new ArrayList<Integer>(path));
return;
} if (target < 0) {
return;
} // 注意,这里从 i = index开始
// 每次只取第一个,例如 123334,到了333这里,我们第一次只取第1个3,因为我们选任何一个3是对组合来说是一个解。所以只
// 第一次取就好了。
for (int i = index; i < num.length; i++) {
int n = num[i];
if (i != index && n == num[i - 1]) {
continue;
}
path.add(n);
dfs(num, target - n, path, ret, i + 1);
path.remove(path.size() - 1);
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/combination/CombinationSum2_1203.java

LeetCode: Combination Sum II 解题报告的更多相关文章

  1. 【LeetCode】40. Combination Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...

  2. LeetCode: Path Sum II 解题报告

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  3. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

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

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

  7. 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】216. Combination Sum III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:h ...

  9. 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

随机推荐

  1. Ubuntu系统安装VMware Tools的简单方法

    不少网友反映在VMWare虚拟机下安装Ubuntu系统后无法安装VMware Tools,这里给出一个简单方法,只需要几步即可解决. 第一步:进入系统后,点击虚拟机上的安装vmware tools,回 ...

  2. eval、exec、execfile

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #http://blog.csdn.net/azhao_dn/article/details/6921654 ...

  3. Synchronized和Lock, 以及自旋锁 Spin Lock, Ticket Spin Lock, MCS Spin Lock, CLH Spin Lock

    Synchronized和Lock synchronized是一个关键字, Lock是一个接口, 对应有多种实现. 使用synchronized进行同步和使用Lock进行同步的区别 使用synchro ...

  4. 开源大数据技术专场(下午):Databircks、Intel、阿里、梨视频的技术实践

    摘要: 本论坛第一次聚集阿里Hadoop.Spark.Hbase.Jtorm各领域的技术专家,讲述Hadoop生态的过去现在未来及阿里在Hadoop大生态领域的实践与探索. 开源大数据技术专场下午场在 ...

  5. Java开源内容管理CMS系统J4CMS集成到JTM

    JTM是Win32下绿色免费的JDK + Tomcat + MySQL环境集成工具. 通过JTM用户无需对JDK.Tomcat.MySQL进行不论什么安装和配置就可以迅速搭建支持JSP + MySQL ...

  6. UpdatePanel的用法详解

    摘自:http://www.cnblogs.com/shangxia/articles/2281782.html 一.UpdatePanel的结构 <asp:ScriptManager ID=& ...

  7. SQL plus连接远程Oralce数据库

    如果要连接远程数据库,传统的一定可行的方法是在本地装一个oracle,然后使用“Network Configuration Assistant”配置,之后用PL/SQL Dev连接 oracle官网上 ...

  8. 戴尔大力宣传Ubuntu 对比与Windows的差异

    2010-06-18 10:58:36   11175 人阅读   作者:萧萧 编辑:萧萧[爆料]  评论(46)   戴尔近日上线了一个新的网页,对比Linux开源系统(主要是Ubuntu)与Win ...

  9. Mac新建文件夹、txt文件、其他格式文件

    Mac新建txt,正好有人问我,我就把我自己的方法记录一下: 先cd到你指定的文件路径下: 新建文件夹: mkdir test 新建txt touch test.txt 新建无后缀格式文件 touch ...

  10. Linux Pin Control 子系统

    Pin Control Subsystem是Linux内核抽象出的一套用于控制硬件引脚的一套子系统. 1.源文件列表 源码位于linux/drivers/pinctrl目录下,源文件列表如下: 文件名 ...