Question

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 -- DFS

Similar solution as Combination Sum.

 public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i = 0; i < candidates.length; i++) {
List<Integer> record = new ArrayList<Integer>();
record.add(candidates[i]);
dfs(candidates, i, target - candidates[i], result, record);
}
return result;
}
private void dfs(int[] candidates, int start, int target, List<List<Integer>> result, List<Integer> record) {
if (target < 0)
return;
if (target == 0) {
if (!result.contains(record))
result.add(new ArrayList<Integer>(record));
return;
}
// Because C can only be used once, we start from "start + 1"
for(int i = start + 1; i < candidates.length; i++) {
record.add(candidates[i]);
dfs(candidates, i, target - candidates[i], result, record);
record.remove(record.size() - 1);
}
}
}

Solution 2 -- BFS

To avoid duplicates in array, we can create a class:

class Node {

  public int val;

  public int index;

}

And put these node in arraylists for BFS.

Combination Sum II 解答的更多相关文章

  1. 【leetcode】Combination Sum II

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

  2. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

  3. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

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

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

  5. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  6. 【LeetCode】40. Combination Sum II (2 solutions)

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

  7. LeetCode: Combination Sum II 解题报告

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

  8. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  9. leetcode-combination sum and combination sum II

    Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...

随机推荐

  1. Web UI 网站用户界面设计命名规范

    Web UI 网站用户界面设计命名规范 WEB UI设计命名规范,也就是网站用户界面设计(网页设计)命名规范. 这套规范并非单纯的CSS.html或JavaScript命名规范,它涉及了很多使用Pho ...

  2. 恢复Linux下被误删除的文件(笔记)

    恢复Linux下被误删除的文件 [root@xuegod63 ~]# mount /dev/cdrom /mnt/ 分一个区:sda4  查找:extundelete 分一个区:sda4  [root ...

  3. 04747_Java语言程序设计(一)_第7章_图形、图像与多媒体

    例7.1小应用程序用6种字型显示字符串,显示内容说明本身的字型. import java.applet.*; import java.awt.*; public class Example7_1 ex ...

  4. poj 1759 Garland (二分搜索之其他)

    Description The New Year garland consists of N lamps attached to a common wire that hangs down on th ...

  5. python之路-随笔 python处理excel文件

    小罗问我怎么从excel中读取数据,然后我百了一番,做下记录 以下代码来源于:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html ...

  6. Java连接Oracle数据库的示例代码

    最基本的Oracle数据库连接代码(只针对Oracle11g): 1.右键项目->构建路径 ->配置构建路径,选择第三项“库”,然后点击“添加外部Jar”,选择 “D:\Oracle\ap ...

  7. Traceroute原理介绍

    一.路由追踪 路由跟踪,就是获取从主机A到达目标主机B这个过程中所有需要经过的路由设备的转发接口IP. 二.ICMP协议 Internet控制报文协议(internet control message ...

  8. java框架BeanUtils及路径问题练习

    内省----->一个变态的反射    BeanUtils主要解决 的问题: 把对象的属性数据封装 到对象中.  使从文件中读取的数据往对象中赋值更加简单:   BeanUtils的好处:  1. ...

  9. 把Go程序变小的办法

    把Go程序变小的办法是: go build -ldflags “-s -w” (go install类似) -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了, 这 ...

  10. iOS 8 Metal Swift教程(一) :开始学习

    在本篇教程中,你将应用到3D图形中的一系列矩阵变换,并会学习到如下内容: 如何使用模型(model),视图(view)以及投影变换(projection transformations). 如何使用矩 ...