LeetCode 40. Combination Sum II 组合总和 II (C++/Java)
题目:
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]
]
分析:
本题是LeetCode 39. Combination Sum 组合总和 (C++/Java)的扩展,我们还是利用39题中的方法来做这道题。
只不过本题所给的数字有重复,且要求最后的结果中不能有重复的,例如例子1,有两个1,他们都可以和7组合成8,如果按照39题搜索的方法就会产生重复结果。
其中一个办法就是将搜索生成的结果加入到set中,这样集合类会自动帮我们去重。
此外我们还可以在每一轮搜索时,当发现有相同元素时,直接跳过本次搜素,这样就不会产生重复的结果了。
程序:
C++
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> curr;
sort(candidates.begin(), candidates.end());
dfs(candidates, target, 0, res, curr);
return res;
}
void dfs(vector<int>& candidates, int target, int index, vector<vector<int>>& res, vector<int> curr){
if(target == 0){
res.push_back(curr);
return;
}
for(int i = index; i < candidates.size(); ++i){
if(candidates[i] > target)
return;
if(i > index && candidates[i] == candidates[i-1])
continue;
curr.push_back(candidates[i]);
dfs(candidates, target-candidates[i], i+1, res, curr);
curr.pop_back();
}
}
};
Java
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> curr = new LinkedList<>();
Arrays.sort(candidates);
dfs(candidates, target, 0, res, curr);
return res;
}
private void dfs(int[] candidates, int target, int index, List<List<Integer>> res, LinkedList<Integer> curr){
if(target == 0){
res.add(new LinkedList<>(curr));
return;
}
for(int i = index; i < candidates.length; ++i){
if(candidates[i] > target)
return;
if(i > index && candidates[i] == candidates[i-1])
continue;
curr.addLast(candidates[i]);
dfs(candidates, target-candidates[i], i+1, res, curr);
curr.removeLast();
}
}
}
LeetCode 40. Combination Sum II 组合总和 II (C++/Java)的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [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 ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [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] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- LeetCode 40. 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)
这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
随机推荐
- js 校验手机号与校验邮箱正则表达式
js 校验手机号与校验邮箱正则表达式 以下 checkMobile(mobile) { var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+ ...
- 一首歌的时间,手把手搭建基于FC的网站
简介: 不是杰伦的那一首歌啦~ 部署网站 说好不哭 在接触serverless架构之前,我们如果想实现上线一个Web网站,就要在开发前期经过操作很多冗杂但又必须的步骤,不少小白可谓是快速的从入门到退坑 ...
- 来电科技:基于 Flink + Hologres 的实时数仓演进之路
简介: 本文将会讲述共享充电宝开创企业来电科技如何基于 Flink + Hologres 构建统一数据服务加速的实时数仓 作者:陈健新,来电科技数据仓库开发工程师,目前专注于负责来电科技大数据平台离线 ...
- [Auth] 浅谈 jwt token 的妙处
无状态(易扩展). 有过期时间限制,相对安全(可以有多个有效的 token). 更轻量(适合少量信息),类似传统 query string 签名方式. 标准统一(跨语言). Refer:JWT Aut ...
- WPF 已知问题 Separator 无法应用 ContextMenu 定义的默认样式
本文记录一个 WPF 已知问题,在 ContextMenu 的 Resources 里定义 Separator 的默认样式,在 ContextMenu 里面的 Separator 将应用不上,或者说不 ...
- 设计模式之装饰器-AOP
HelloWorld简单例子如下:此例子好好体会下继承 is a和组合 has a的异同. using System; using System.Runtime.InteropServices; na ...
- C语言程序设计-笔记7-指针
C语言程序设计-笔记7-指针 例8-1 利用指针模拟密码开锁游戏. #include<stdio.h> int main(void) { int x=5342; //变 ...
- css的animate做一个信号动画
html <div class="jump flex-fs fadeAndScaleIn"> <span></span> <span> ...
- AI实战 | 手把手带你打造智能待办助手
背景 大家好,我是努力的小雨.今天我想分享一下搭建待办助手的经历.起初,我并没有什么特别的创意点子.但在4月16日的百度Create大会上,我看到了小度的大模型加持使得其变得更加智能.我被一场示例所震 ...
- ITSM2023年十大功能趋势[采和]
总体描述:更加人性化,引入自动化相关的设计和技术,更加实用好用.1. 100%服务目录服务目录必须完全贴合用户方的运维实际开展的 服务清单,而不是想当然的抄书或者臆想!都2023年了,还有完全不着调的 ...