Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

Each number in candidates may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

Example 2:

Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]

题意:

给定一个集合以及一个值target,找出所有加起来等于target的组合。(每个元素只能用一次)

Solution1: Backtracking

[leetcode]39. Combination Sum组合之和的基础上, 加一行查重的动作。

code

 class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
helper(candidates, 0, target, path, result );
return result;
} private void helper(int[] nums, int index, int remain, List<Integer> path, List<List<Integer>> result){
if (remain == 0){
result.add(new ArrayList<>(path));
return;
}
for(int i = index; i < nums.length; i++){
if (remain < nums[i]) return;
if(i > index && nums[i] == nums[i-1]) continue; /** skip duplicates */
path.add(nums[i]);
helper(nums, i + 1, remain - nums[i], path, result);
path.remove(path.size() - 1);
}
}
}

[leetcode]40. Combination Sum II组合之和之二的更多相关文章

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

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

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

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

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

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

  4. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

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

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

  7. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  8. LeetCode OJ:Combination Sum II (组合之和 II)

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

  9. LeetCode 40. Combination Sum II (组合的和之二)

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

随机推荐

  1. golang: 利用unsafe操作未导出变量

    unsafe.Pointer其实就是类似C的void *,在golang中是用于各种指针相互转换的桥梁.uintptr是golang的内置类型,是能存储指针的整型,uintptr的底层类型是int,它 ...

  2. 阅读 video in to axi4-stream v4.0 笔记

    阅读 video in to axi4-stream v4.0 笔记 axi4 stream里面只传输的有效数据. 引用: 使能了video timing controller core 的所用信号, ...

  3. debian下arp欺骗

    sudo sysctl -w net.ipv4.ip_forward= sudo sysctl -p arpspoof -i eth0 -t 目标ip -r 伪装ip或者ettercap -i eth ...

  4. JNDI在Spring和tomcat下的使用

    1. 是什么 JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一.JNDI 在 J2EE 中的角色就是&q ...

  5. 轻松制作X86 OPENWRT USB启动盘

    本文介绍了一个x86 live USBi启动盘的制作方法. 该方法有如下特点: 1.  可在winXP/win 7/win vista上制作, U盘采用fat格式, 即使对于linux经验较少者, 也 ...

  6. git 退回到前面某一版本的具体操作方式

  7. iOS开发SDWebImageOptions理解

    iOS开发SDWebImageOptions理解 原文 http://www.cnblogs.com/WJJ-Dream/p/5816750.html typedef NS_OPTIONS(NSUIn ...

  8. 验证Textbox的字符长度

    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { ) { //Indi ...

  9. vue-cli 选项无法选问题

    winpty vue.cmd create admin 这样创建就可以了

  10. [Oracle,2018-03-01] oracle常用函数

    最近经常用到一些oracle中的函数,今天就总结一些常用的: 一.单行函数 只处理单个行,并且为每行返回一个结果. 1.字符函数 (1)concat(str1,str2)字符串拼接函数 select ...