[Leetcode 40]组合数和II Combination Sum II
【题目】
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.
 
- All numbers (including 
 
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的更多相关文章
- [Leetcode 39]组合数的和Combination Sum
		
[题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...
 - 【Leetcode】【Medium】Combination Sum II
		
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
 - 【leetcode刷题笔记】Combination Sum II
		
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
 - 【LeetCode每天一题】Combination Sum II(组合和II)
		
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
 - [Swift]LeetCode40. 组合总和 II | Combination Sum II
		
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
 - [Leetcode][Python]40: Combination Sum II
		
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
 - [array] leetcode - 40. Combination Sum II - Medium
		
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
 - Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
		
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
 - leetcode 39. Combination Sum  、40. Combination Sum II 、216. Combination Sum III
		
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
 
随机推荐
- javascript 之  第七章第一节(递归)
			
先举例: function factorial(num) { ) { return num; } else { ); } } //输出120 //进一步去思考有名字的函数,且名字不会有变化的情况下,这 ...
 - 【我的前端自学之路】【HTML5】web 存储
			
以下为自学笔记内容,仅供参考. 转发请保留原文链接:https://www.cnblogs.com/it-dennis/p/10503539.html 什么是Web存储 cookie最大的缺陷是在每一 ...
 - selenium+unittest自动化测试
			
学了unittest+接口测试后,又试着用框架去做UI测试.感觉还是很好用的. 项目里该有的基本都有了,供以后扩展学习做个参考. 链接:https://github.com/Mollylin0/mon ...
 - Vue-admin工作整理(十四):Vuex和双向绑定
			
概述,普通的直接通过input修改值然后取是不符合vue的规格的,所有数据定义和传递必须通过actions或者mutation来做 思路:通过在mutation层对字段进行定义值,在store中通过v ...
 - Economics degrees
			
Economics degrees Name game"> 经济学学位"> 名称痕戏 Luring students with a new label 新瓶旧酒吸引学生 ...
 - 【NET Core】Nuget包发布流程
			
1.新建一个.NET Core类库 2.新增一个方法,并编译项目 3.下载Nuget.exe,与刚才新建的类库放在同一目录下 下载地址:https://www.nuget.org/downloads ...
 - ES6删除对象中的某个元素
			
const form = { id: '011', name: '测试一', description: '测试demo' } // 目标: 取到删除description属性的对象, 即下文的data ...
 - redis缓存中间件基础
			
前序: 默认使用SimpleCacheConfiguration 组件ConcurrentMapCacheManager==ConcurrentMapCache将数据保存在ConcurrentMap& ...
 - 使用PsPing测试Azure虚拟机的连通性
			
Azure虚拟机启动后,如果在个人的PC上ping该虚拟机的public IP,会出现Request time out的信息,无法ping通.这是因为在 Azure 中,ICMP 包无法通过防火墙和负 ...
 - python两个列表合并为字典,一个作为key,一个作为value
			
两个列表合并为一个字典函数list_dic(list1,list2)可以直接复制拿走 传入的参数为两个列表,list1准备作为key,list2准备作为value,key和value位置一一对应. d ...