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. html+css web storage课上笔记 2019.3.18

    存储 cookie cookie 使用文本来存储信息 使用时服务器发送cookie给客户端,下一次时,浏览器发送给服务器 web storage local storage 本地的硬件设备中,关闭后不 ...

  2. 十六进制转化二进制[c]

    #include<stdio.h> #include<string.h> #include<stdlib.h> int hex2dec(char c) { ; // ...

  3. Python笔记_第四篇_高阶编程_二次封装

    1.二次封装: 二次封装其实就是对一个类或者一个方法进行二次的改造增加新的功能. 2.一个类的二次封装: 我们以一个进程为例,我们把Process这个库进行二次封装,增加一些功能,在调用. thoma ...

  4. 前端Json 增加,删除,修改元素(包含json数组处理)

    一:基础JSON对象  二:JSON数组数据 以下为增删修改方法: <!DOCTYPE html> <html lang="en"> <head> ...

  5. [Algo] 117. Array Deduplication III

    Given a sorted integer array, remove duplicate elements. For each group of elements with the same va ...

  6. Mybatis配置文件无故报错、无自动完成提示的解决方法,及自动生成主要配置项

    1.引子 Mybatis配置文件显示红叉有错误,而实际检查又没有错误,这是因为开发环境不能识别这种类型的xml文件.要解决这个问题,就要让IDE开发环境能够“认识”这个文件类型,我们要让IDE环境将这 ...

  7. 使用tcpdump查看HTTP请求响应 详细信息 数据

    安装tcpdump: sudo yum install tcpdump 查看get请求: tcpdump -s 0 -A 'tcp dst port 80 and tcp[((tcp[12:1] &a ...

  8. Django搭建后篇——启动服务器及创建视图

    开启服务器,Django开启服务器的方式有两种,一种是在Ubuntu在开启,另一种是直接在pycharm开启.就方便而言肯定是第二种,但由于pycharm版本的问题,可能有的人无法直接在pycharm ...

  9. nginx配置文件说明(包含IP黑名单、代理反射、负载均衡的配置)

    先看下nginx配置文件整体结构 图片来源51cto 配置文件及注解: #运行用户 主模块指令,指定Nginx Worker进程运行用户以及用户组,默认由nobody账号运行 user nobody; ...

  10. HTML5之FileReader小结

    方法: 方法名 参数 描述 abort none 中断读取 readAsBinaryString file 将文件读取为二进制码 readAsDataURL file 将文件读取为 DataURL r ...