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]
] Time: O(2^N)
Space: O(N)
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return res;
}
Arrays.sort(candidates);
List<Integer> lst = new ArrayList<>();
helper(res, lst, candidates, target, 0);
return res;
} private void helper(List<List<Integer>> res, List<Integer> lst, int[] candidates, int reminder, int index) {
if (reminder < 0) {
return;
}
if (reminder == 0) {
res.add(new ArrayList<>(lst));
}
for (int i = index; i < candidates.length; i++) {

         //i > index will only skip when numbers before cur is used.
        // i > 0 skip all one combination like [1, 1, 6]

            if(i > index && candidates[i] == candidates[i - 1]) {
continue;
}
lst.add(candidates[i]);
helper(res, lst, candidates, reminder - candidates[i], i + 1);
lst.remove(lst.size() - 1);
}
}
}

[LC] 40. Combination Sum II的更多相关文章

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

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

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

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

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

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

  4. 【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 ...

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

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

  6. 【LeetCode题意分析&解答】40. Combination Sum II

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

  7. LeetCode OJ 40. Combination Sum II

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

  8. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  9. [leetcode]40. Combination Sum II组合之和之二

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

随机推荐

  1. nginx基础知识小结

    配置文件讲解: #user nobody; #开启进程数 <= CPU数 worker_processes 1; #错误日志保存位置 #error_log logs/error.log; #er ...

  2. springcloud之Eureka上

    0 环境 系统环境:win10 编辑器:IDEA 1 注册中心 Eureka是springcloud中的注册中心.原因: 当是单体应用 类似一条直线 随着项目越来越大 系统拆分 类似那个藕(模块间相互 ...

  3. jenkins忘记登录密码解决方法

    第一步:修改配置文件 修改jenkins的配置文件,找到如下几行删除(删除前一定要备份) <useSecurity>true</useSecurity> <authori ...

  4. C/C++源程序到可执行程序的过程

    源程序.cpp  预处理得到 预处理文件.i   编译得到 汇编文件.S    汇编得到 目标文件.o     链接得到 可执行文件 例子:main.cpp  fun.cpp fun.h #inclu ...

  5. VCPU的解释

    vCPU,顾名思义,是虚拟CPU. 创建虚拟机时,需要配置vCPU资源. 因此vCPU是虚拟机的部件. 因此脱离VM,谈论vCPU是没有意义的. 虚拟化管理系统如何调度vCPU,取决于系统内的虚拟机数 ...

  6. 18 11 15 网络通信 ---- 多任务----线程 threading

    下面是一个  多线程  运算  调用了 threading  模块   可以同时在一个程序中  跑两个函数 import threading def text1 (): for i in range( ...

  7. IPO套路

    日前,温州市冠盛汽车零部件集团股份有限公司(以下简称:冠盛集团)在证监会官网更新了招股说明书,距离上会仅一步之遥.值得注意的是,这已经是公司第四次披露招股说明书,2018年6月,公司曾在IPO审核最严 ...

  8. 吴裕雄--天生自然 JAVA开发学习:条件语句

    public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out. ...

  9. PAT Basic 1104 数字⿊洞 (20) [数学问题-简单数学]

    题目 给定任⼀个各位数字不完全相同的4位正整数,如果我们先把4个数字按⾮递增排序,再按⾮递减排序,然后⽤第1个数字减第2个数字,将得到⼀个新的数字.⼀直重复这样做,我们很快会停在有"数字⿊洞 ...

  10. OpenVINO在linux下安装

    一.准备安装环境: 1.带命令行的64位centos操作系统,推荐极简安装模式,先不要安装GNOME桌面,避免和后面的openvino环境冲突 2.cpu要求 我目前是在服务器的虚拟机里面安装 注意如 ...