[leetcode]40. 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.
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]
]
题意:
给定一个集合以及一个值target,找出所有加起来等于target的组合。(每个元素只能用一次)
Solution1: Backtracking
在[leetcode]39. Combination Sum组合之和的基础上, 加一行查重的动作。
code
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
helper(candidates, 0, target, path, result );
return result;
}
private void helper(int[] nums, int index, int remain, List<Integer> path, List<List<Integer>> result){
if (remain == 0){
result.add(new ArrayList<>(path));
return;
}
for(int i = index; i < nums.length; i++){
if (remain < nums[i]) return;
if(i > index && nums[i] == nums[i-1]) continue; /** skip duplicates */
path.add(nums[i]);
helper(nums, i + 1, remain - nums[i], path, result);
path.remove(path.size() - 1);
}
}
}
[leetcode]40. Combination Sum II组合之和之二的更多相关文章
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- PHPSTORM ACTIVATION 注册激活
最近新出了PHPSTORM10,于是把自己机器上的升级了下.这家伙收费的,国人嘛...你懂的. 安装后,发现不能用原来的keygen注册激活了,于是Google了一下,下面是解决方案: 安装好打开的时 ...
- Docker进入容器后使用ifconfig等命令“command not found”解决办法
当进入一个容器后,使用ifconfig.ip addr等命令时,出现如下“command not found”: 解决办法: yum update yum -y install n ...
- Visual Studio安装SVN插件
VS的SVN插件 材料 VS安装程序. VisualSVN安装程序,点击下载. VisualSVN-5.0.1 前期准备 在代码管理的服务器上安装SVN server,可参考svn安装部署以及服务器 ...
- docker4种网络最佳实战 --摘自https://www.cnblogs.com/iiiiher/p/8047114.html
考: http://hicu.be/docker-container-network-types docker默认3中网络类型 参考: https://docs.docker.com/engine/u ...
- [转]c#快捷键
c#快捷键(成为高手必备) CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 CTRL + SHIFT ...
- reids高可用(灾难备份-持久化)
java缓存存放到内存之中,当服务器重启以后,内存的数据将丢失,而reids作为缓存,重启reids以后 数据是不是也会丢失,redis服务器重启以后数据也不会丢失,这个是redis提供了持久化的功能 ...
- Excel函数匹配查找
需求 例如:北京沃尔玛有限公司,由已知的沃尔玛缩写,将两者进行匹配. 函数 lookup() 第一个参数“Lookup_value”:是要查找的值:第二个参数“lookup_vector”:是要查找的 ...
- ARM 编译产生.map之RO RW ZI
RO: 程序中的指令和常量,存储在常量和代码区,因为是不能改变的,所以—READ ONLY; RW: 程序中的已初始化全局变量和静态变量,存储在堆中,因为是变量,所以——READ AND WRITE ...
- linux怎么样显示命令历史后又显示命令的输入时间
linux的bash内部命令history就可以显示命令行的命令历史,默认环境执行 history命令后,通常只会显示已执行命令的序号和命令本身.如果想要查看命令历史的时间戳,那么可以执行: 临时显示 ...
- Hibernate Criteria使用
hibernate中Criteria的完整用法 Criteria 是一个完全面向对象,可扩展的条件查询API,通过它完全不需要考虑数据库底层如何实现.SQL语句如何编写,是Hibernate框架的核心 ...