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也是正数. ...
随机推荐
- 力扣303(java)-区域和检索-数组不可变(简单)
题目: 给定一个整数数组 nums,处理以下类型的多个查询: 计算索引 left 和 right (包含 left 和 right)之间的 nums 元素的 和 ,其中 left <= rig ...
- 力扣32(java)-最长有效括号(困难)
题目: 给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度. 示例 1: 输入:s = "(()"输出:2解释:最长有效括号子串是 &quo ...
- CF1857B Maximum Rounding 题解
题目描述 给定一个自然数 \(n\),可以对任意一位进行四舍五入,可以进行任意次,求能得到的最大数.(这里的 \(n\) 没有前导零) 思路 首先我们发现,如果我们将其中一位进位了,那后面的所有位都会 ...
- [Contract] Solidity 合约发布到测试网 ropsten 的作用
当我们本地完成了一系列测试以后,接下来就是准备上线了. 关于合约部署可以参考这篇:Solidity 合约使用 truffle 部署到测试网和主网 你可能有一个疑问,在上主网之前,先上测试网的作用是什么 ...
- WPF 让窗口激活作为前台最上层窗口的方法
在 WPF 中,如果想要使用代码控制,让某个窗口作为当前用户的输入的逻辑焦点的窗口,也就是在当前用户活动的窗口的最上层窗口,默认使用 Activate 方法,通过这个方法在大部分设备都可以做到激活窗口 ...
- 2019-8-31-C#-大端小端转换
title author date CreateTime categories C# 大端小端转换 lindexi 2019-08-31 16:55:58 +0800 2018-05-28 10:21 ...
- C#/C++ 通过ODBC连接OceanBase Oracle租户
概述 近期我们项目正处于将Oracle数据库迁移到OceanBase Oracle租户模式的阶段.考虑到我们项目采用了C++和C#混合开发,并且使用了多种技术,因此存在多种数据库连接方式.然而,针对C ...
- Python的国内安装源(也称为镜像源)
Python的国内安装源(也称为镜像源)数量会随着时间而增加或减少,因为新的镜像源可能会建立,而一些旧的镜像源可能会停止服务或不再更新.以下是一些常用的Python国内安装源(也称为PyPI镜像源): ...
- Xcode多进程调试:WKWebView
由于WKWebView使用的是多线程架构,渲染模块和网络模块都各自在一个单独的进程里面,因此,如果需要设置渲染模块或者网络模块里面的断点,需要做一些特殊处理. 举个例子,假设在Xcode里面设置了渲染 ...
- IDEA文件夹注释插件TreeInfotip使用
目录 前景提要 环境整合 构建工具(参考工具部署方式) 下载插件 使用 前景提要 很多开源代码或者公司代码,因为层级比较多,所以查阅困难,发现一个TreeInfotip插件可以对这样的文件做注释 环境 ...