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. redis 的备份策略,最好使用:RDB-AOF 混合持久化

    相关资料: Redis 4.0 新功能简介:RDB-AOF 混合持久化:http://blog.huangz.me/2017/redis-rdb-aof-mixed-persistence.html ...

  2. ubuntu18关闭系统自动更新

    ubuntu18.04关闭系统自动更新有两个方法:1.修改配置文件 修改配置文件/etc/apt/apt.conf.d/10periodic#0是关闭,1是开启,将所有值改为0vi etc/apt/a ...

  3. 对象克隆(C# 快速高效率复制对象另一种方式 表达式树转)

    1.需求 在代码中经常会遇到需要把对象复制一遍,或者把属性名相同的值复制一遍. 比如: public class Student { public int Id { get; set; } publi ...

  4. 函数和对象 及 prototype和__proto__

    对象有  __proto__ 函数有 prototype 对象的__proto__指向构造自己的函数的prototype 但有一例外 var Obj = {v:99}var pObj = Object ...

  5. 【java】模板方法设计模式

    模板方法:在定义功能时,功能一部分是确认的,另一部分是不确认的或者后续会变化的.这时可以把不确定的部分暴露出去,定义成抽象类或者接口,由子类来完成. abstract class GetDuring ...

  6. Parallel Programming for FPGAs 学习笔记(1)

    Parallel Programming for FPGAs 学习笔记(1)

  7. easyui layout布局的属性说明

    layout布局的属性说明: 名称 类型 描述 默认值 fit boolean 当设置为 true 时,就设置布局(layout)的尺寸适应它的父容器.当在 'body' 标签上创建布局(layout ...

  8. time&datetime

    关于time模块的代码部分 1 #_*_coding:utf-8_*_ 2 __author__ = 'Alex Li' 3 4 import time 5 6 7 # print(time.cloc ...

  9. sitemesh使用

    参考文章: https://my.oschina.net/heroShane/blog/199177 http://blog.csdn.net/u013019926/article/details/1 ...

  10. c#继承 里氏转化原则

    继承: 是c#中面向对象一个重要概念: 用一个已经存在的类去定义一个新的类 新的类叫做   子类/派生类 已经存在的类叫做   父类/基类 c#中所以类的最终基类都是Object类 声明 访问修饰符  ...