[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 ...
随机推荐
- 0x14哈希之兔子兔子
参考链接:https://www.cnblogs.com/wyboooo/p/9813428.html 题目链接:https://www.acwing.com/problem/content/140/ ...
- java如何编写多线程
1.如何实现多线程 1.1实现Runnable接口,实现run()方法. public class Main4 implements Runnable { public static void mai ...
- 如何加速GitHub访问速度
http://tool.chinaz.com/网站中填入assets-cdn.github.com选取响应最小的ip,将ip.域名填入到C:\Windows\System32\drivers\etc下 ...
- bzoj2131 免费的馅饼——树状数组优化dp
中文题目,问你最后能最多够得到多少价值的馅饼.因为宽度10^8且个数为10^5.所以不可以用dp[x][y]表示某时间某地点的最大权值. 假设你在x点处接到饼后想去y点接饼.那么需要满足的条件是t[y ...
- loadrunner常用函数集锦
一.三个复制函数的区别: strcpy 原型:extern char *strcpy(char *dest,char *src);用法:#i nclude功能:把src所指由NULL结束的字符串复制到 ...
- Docker介绍及使用
什么是容器? 容器就是在隔离的环境运行的一个进程,如果进程停止,容器就会销毁.隔离的环境拥有自己的系统文件,ip地址,主机名等,kvm虚拟机,linux,系统文件 程序:代码,命令 进程:正在运行的程 ...
- Gradle 简记
不是 Gradle,就是 Maven吧.对比下: Maven: 推荐(?)了一个默认的项目结构和生命周期,但是太过死板 虽然暴露了 API 接口,但是插件定制太过复杂 和 Ant 一样,仍然无法表达复 ...
- Git初识学习
初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 使用命令git add <file>,注意,可反复多次使用,添加多个文件: 使用命令git commit ...
- Mac安装python3.x+pycharm+elasticsearch+常见报错处理(1)
---恢复内容开始--- mac安装python有两种方式:此处叙述官网安装下载. mac 自带python2.x版本,python2和3之间的差距还是有的.下面简单叙述我安装python3.7的过程 ...
- js转盘大抽奖 自定义概率
公司项目搞优惠活动,让做一个转盘抽奖的活动,转盘抽奖让他转起来 按照概率停止其实都麻烦,但是概率如果设置在前端就会很大的安全漏洞,所以无论为了安全性还是后期的维护问题都要把概率写到后台配置里然后读取配 ...