[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 ...
随机推荐
- NumPy 基础用法
NumPy 是高性能科学计算和数据分析的基础包. 它是 pandas 等其他各种工具的基础. 主要功能: ndarray 一个多维数组结构, 高效且节省空间 无需循环对整组数据进行快速运算的数学函数 ...
- Redis使用规范
突出强调部分 [强制]key名不要包含特殊字符,如空格.换行.单双引号以及其他转义字符 [强制]拒绝bigkey(防止网卡流量.慢查询) [强制]控制key的生命周期,redis不是垃圾桶 [强制]技 ...
- Dynamics 365 CRM Free up storage 清理Dynamics 365 CRM的空间
Dynamics 365 CRM 的空间是要买的. 但是很多情况下用户可以去清理CRM从而达到给空间减重的方法 两大使用DB空间大的功能 1. Audit log 审计记录 审计记录是用来记录各个fi ...
- 在 sql 语句出现 warning 之后,立刻执行 `show warnings;` 就可以看到 warning 提示信息
在 sql 语句出现 warning 之后,立刻执行 show warnings; 就可以看到 warning 提示信息
- Transaction rolled back because it has been marked as rollback-only 原因 和解决方案
产生原因 , 1 serviceA 调用 serviceB 然后 B 抛出异常 ,B 所在的 事物 回滚,B 把当前可写 事物标记成 只读事物 , 2 如果 A 和B 是在 同一个事物环境,并且 ...
- NPOI 操作Word
/// <summary> /// 替换word中指定内容 /// </summary> /// <param name="wordPath"> ...
- Azure VMSS (1) 入门
<Windows Azure Platform 系列文章目录> 在使用云计算服务的时候,我们经常需要有自动横向扩展的功能.比如: 1.在业务高峰期,根据负载的增加,自动打开若干台VM 2. ...
- 廖雪峰Java7处理日期和时间-3java.time的API-1LocalDateTime
1.java.time提供了新的日期和时间API: LocalDate/LocalTime/LocalDateTime ZoneDateTime/ZoneId Instant Formatter 新A ...
- [UE4]VR成像原理
一.双眼成像原理 二.3D电影成像原理 模拟人眼.用2个摄像机拍摄,模拟人的左眼和右眼 播放的时候2个投影仪分别同时播放左右摄像机拍摄到内容,观众带上3D眼镜,左眼只能看到左摄像机的内容(过滤右摄像机 ...
- vue里面axios使用post
let params = new URLSearchParams(); params.append('action', "login"); params.append('user' ...