【题目】

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. Native App自动化测试及Appium框架介绍

    一  自动化工具简介 1 Appium : 开源,跨平台的自动化测试工具,用于测试Native(原生)和Hybrid(混合)应用,支持IOS/Android/FirefoxOS 平台. 2  环境 : ...

  2. UVA1479 Graph and Queries

    思路 恶心人的题目 还是类似永无乡一题的Treap启发式合并思路 但是由于加边变成了删边 所以应该离线后倒序处理 数组要开够 代码 #include <cstdio> #include & ...

  3. java 随机出题四则运算

    作业要求来源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2186 我的github地址:https://github.com/k ...

  4. <工厂方法>比<简单工厂>多了啥

    前言:多注重设计.仅当复习讨论! 简单工厂模式 UML图   假如有一位爱心人士,想给饥饿的流浪动物喂食.此时爱心人士身带了狗粮,但是他到处找啊找,最终只找到了猫大人,是不是有点惨兮兮.但是如果有简单 ...

  5. 微信、qq二次分享

    前言 我们平时做微信分享的时候,一般分享出来的页面都是一个简单的html页面,不会加入框架之类的东西.所以当我们在分享出来的页面里面再次进行分享的时候,由于我们没有配置分享的标题.描述这些东西,分享出 ...

  6. php安全开发(1)文件包含漏洞

    开发过程总结的漏洞: 一,,如何造成包含漏洞:在通过函数包含文件时,由于没有对包含的文件名进行有效的过滤处理,被攻击者利用从而导致了包含了Web根目录以外的文件进来,就会导致文件信息的泄露甚至注入了恶 ...

  7. CDH 报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordinal not in range

    1.在CDH集群启动Hue服务时,出现了错误,如下图: 2.上图显示得知,是调用python文件(/opt/cloudera/parcels/CDH-5.16.1-1.cdh5.16.1.p0.3/l ...

  8. LInux 些许知识

    1.Linux下去掉^M的方法 ①dos2unix filename ②sed -i 's/^M//g' filename #注意:^M的输入方式是 Ctrl + v ,然后Ctrl + M 2.so ...

  9. web前端常见面试题

    转载自:https://www.cnblogs.com/jj-z/p/7999538.html 一.理论知识 1.1.讲讲输入完网址按下回车,到看到网页这个过程中发生了什么 a. 域名解析 b. 发起 ...

  10. C# 一些不注意知识点:命名空间,等级,class等等

    C# 命名空间表示域,控制着域内的对象. 命名空间是第一等级,class,delegate,enum,interface,struct是第二等级: 方法,属性,字段,索引,事件,常量,构造函数,终结器 ...