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. 工具:BT Sync 同步文件

    随着互联网的发展,文件共享变得越来越便捷,但是文件的共享过程是不是安全,这一直是人们关心的问题,今天向大家介绍一个共享工具,可以实现便捷的安全共享.      这个工具分为两个部分,一个是服务器部分, ...

  2. 精通find命令

    一.前言 find命令是linux使用过程中经常用到的命令,但可能大家只会如下使用find find ./ 或者这样使用 find ./ | grep str 上述命令等同于 find ./ -nam ...

  3. [置顶] vi、akw和sed总结

  4. shell数组(产生不同的随机数)

    #!/bin/bash # declare -a ARRAY read -p "Please input num[1-39]:" EMENUM #对比新生成的随机数是否重复 fun ...

  5. hdu 5036 Explosion(概率期望+bitset)

    Problem Description Everyone knows Matt enjoys playing games very much. Now, he to N. Input The firs ...

  6. Spring 反转控制(IOC) 依赖注入(DI)

    简单的理解: 之前是我为了完成业务A 需要某个对象B的支持 那么我在这个业务A中就会创建一个对象B使用,说白了就是我根据需求来控制对象B, 而spring的ioc把对象B的控制权从业务A手中拿到自己手 ...

  7. 07_DICTIONARY_ACCESSIBILITY

    07_DICTIONARY_ACCESSIBILITY 控制对系统权限的限制: TRUE 有相应系统权限,允许访问SYS下的对象. FALSE 确保拥有可以访问任何对象的系统权限,但不可以访问SYS下 ...

  8. Android 平滑图片加载和缓存库 Glide 使用详解

    在图片加载库烂大街的今天,选择一个适合自己使用的图片加载库已经成为了每一个Android开发者的必经之路.现在市面上知名的图片加载库有UIL,Picasso,Volley ImageLoader,Fr ...

  9. Oracle/Mysql/SqlServer函数区别

    mysql日期和时间格式转换 Linux scp 使用详解 Oracle/Mysql/SqlServer函数区别 2011-07-01 12:34:36|  分类: Mysql技术 |  标签:mys ...

  10. ios 动态修改UILabel字体大小

    - (IBAction)sliderChange:(id)sender {   NSLog(@"sliderChange");   UISlider *slider = (UISl ...