[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 ...
随机推荐
- Centos7.4安装配置haproxy和Keepalived
系统版本是centos7.4的 [root@data-1-1 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [roo ...
- sed用法说明
sed介绍 sed:stream editor 是一个行编辑器,或叫流编辑器,每次处理一行,处理完一行再处理下一行.sed并不直接处理源文件,而是读取一行后放入模式空间(patten space)里, ...
- 小程序https请求,http网站升到https
最近开发小程序,因为以前只写过小程序的前端没注意接口,现在才发现原来所有的接口都必须使用https协议了,马上研究了一波,顺便也想给自己的博客升成https的. 申请免费证书 哈哈没办法就是喜欢免费的 ...
- Ubuntu 16.04 安装 JDK 1.8
系统环境 Ubuntu 16.04; JDK 1.8 配置安装 1.首先从oracle下载jdk 1.8,我下载的版本是jdk-8u131-linux-x64.tar.gz,运行tar zvxf jd ...
- 关于分布式uuid的一点设想
在一次公开课上,听别人讲过全局分布式uuid的设计,听过twitter的snowflake的设计.也听过,如果使用单独的计数器服务,不可能每次都保存当前计数器到文本,自己想到应该可以每隔一些数,例如1 ...
- 《女神异闻录 5》的 UI 设计
转自:https://www.zhihu.com/question/50995871?sort=created <女神异闻录5>是近两年最为火热的JRPG游戏之一,它的出色不仅在于剧情暗讽 ...
- django之COOKIE 与 SESSION
COOKIE 与 SESSION 概念 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie ...
- Tomcat的简单归纳总结
2017年08月09日 12:39:23 大道之简 阅读数:1072 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/HcJsJqJSSM/ar ...
- Android Room 学习(一)
Room简介 Room persistence库为SQLite提供了一个抽象层,以便在利用SQLite的全部功能的同时实现更强大的数据库访问. 该库可帮助您在运行应用程序的设备上创建应用程序数据的缓存 ...
- 201772020113 李清华《面向对象程序设计(java)》第三周学习总结
一.测试题反思: 这次的测试题暴露出我在学习上的很多问题:首先,编程能力非常薄弱,编程题目只写出了第一个程序,还因为小问题通不过测试,以后一定要多上手练习,多阅读示例程序.其次,对理论知识的掌握不全面 ...