【题目】

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]
]

【思路】

回溯,关键在去重:sort后nums[i-1]==nums[i] continue。

1、[Leetcode 78]求子集 Subset https://www.cnblogs.com/inku/p/9976049.html

2、[Leetcode 90]求含有重复数的子集 Subset II https://www.cnblogs.com/inku/p/9976099.html

3、讲解在这: [Leetcode 216]求给定和的数集合 Combination Sum III

4、[Leetcode 39]组合数的和Combination Sum

【代码】

有参考39的思路设置全局变量+两种情况合并。

class Solution {
private static List<List<Integer>> ans ;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
ans = new ArrayList<>();
fun(new ArrayList<Integer>(),candidates,target,0);
return ans;
}
public void fun(List<Integer> tmp,int[] data, int aim,int flag){
if(aim<=0){
if(aim==0){
ans.add(new ArrayList<>(tmp));
}
return;
}
for(int i=flag;i<data.length;i++){
if(i>flag&&data[i-1]==data[i])
continue
;
tmp.add(data[i]);
fun(tmp,data,aim-data[i],i+1);
tmp.remove(tmp.size()-1);
}
}
}

[Leetcode 40]组合数和II Combination Sum II的更多相关文章

  1. [Leetcode 39]组合数的和Combination Sum

    [题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...

  2. 【Leetcode】【Medium】Combination Sum II

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

  3. 【leetcode刷题笔记】Combination Sum II

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

  4. 【LeetCode每天一题】Combination Sum II(组合和II)

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

  5. [Swift]LeetCode40. 组合总和 II | Combination Sum II

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

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

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

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

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

  8. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

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

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

随机推荐

  1. Oracle使用——oracle11g安装——Oracle要求的结果: 5.0,5.1,5.2,6.0 6.1 之一 实际结果: 6.2

    问题 正在检查操作系统要求...        要求的结果: 5.0,5.1,5.2,6.0 之一        实际结果: 6.1        检查完成.此次检查的总体结果为: 失败 <&l ...

  2. Eclipse/MyEclipse导入导出注释模板

    1.Eclipse/MyEclipse导入注释模板 (1)打开Eclipse/MyEclipse工具,打开或创建一个Java工程,点击菜单Window->Preferences弹出首选项设置窗口 ...

  3. linux服务基础(二)之httpd2.2基础配置

    一.安装httpd服务 CentOS6 默认安装httpd2.2版本 CentOS7 默认安装httpd2.4版本 # yum install httpd 二.安装后相关文件说明 配置文件: /etc ...

  4. CF932G Palindrome Partition

    思路 首先把字符串变为\(S[1]S[n]s[2]s[n-1] \dots\) 这样原来的一个合法的划分方案就变成了用k个长度为偶数的回文子串划分的方案, 然后直接DP,对i位置,可转移的位置就是它的 ...

  5. 【搬运工】linux下创建用户(一)

    转载:http://www.cnblogs.com/ylan2009/articles/2321177.html linux下创建用户(一) Linux 系统是一个多用户多任务的分时操作系统,任何一个 ...

  6. MySQL自定义函数递归查询

    用于递归查找Id(通过parentId关联)参数为int 类型的值: CREATE DEFINER=`root`@`%` FUNCTION `getChildList`(rootId INT) RET ...

  7. Activiti 工作流之所学所感(基本配置) DAY1

    由于公司需求,最近在研究工作流,在此记录一下所学所感以备往后使用时候可以方便查询,有不足之处请各位大牛提点,下面直接进入主题. 下载activiti 所需资料  可以直接在官网上下载,也可以在我的网盘 ...

  8. 论文阅读-attention-is-all-you-need

    1结构介绍 是一个seq2seq的任务模型,将输入的时间序列转化为输出的时间序列. 有encoder和decoder两个模块,分别用于编码和解码,结合时是将编码的最后一个输出 当做 解码的第一个模块的 ...

  9. Axure下拉列表的交互事件 + 自定义元件库

    下拉列表的交互事件: 场景:当点击第一个下拉列表框的江苏时,第二个列表框会显示江苏省的城市:当点击第一个下拉列表框的北京时,第二个列表框会显示北京市的区 操作:把第二个列表框设置为动态面板,设置为两种 ...

  10. python 写文本文件出现乱码

    最近工作中想完善一下监控日志,同事说客户突然说我们最近几天推送的数据只有几家,赶紧看预警,应推4700多家,实推3400多家,用户可能是看错了,但我记得当时项目验收上线时,这个来源的推送数据肯定是可以 ...