【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列
(一)题目
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
All numbers (including target) will be positive integers
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
(二)解题
具体思路与【一天一道LeetCode】39. Combination Sum这篇博文一样,采用动态规划和回溯法进行求解。
/*
和上一题的思路一样,区别是数字不能重复查找,但Vector中允许有重复的数字
具体改动请看代码注释
*/
class Solution {
public:
vector<vector<int>> ret;
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
for(int idx = 0;idx<candidates.size();idx++)
{
if(idx-1>=0 && candidates[idx] == candidates[idx-1]) continue;//避免重复查找
else
{
if(candidates[idx]<=target)
{//如果小于则调用动态规划函数
vector<int> tmp;
tmp.push_back(candidates[idx]);
combinationDfs(candidates,tmp,idx,target-candidates[idx]);
}
}
}
return ret;
}
void combinationDfs(vector<int>& candidates ,vector<int>& tmp, int start ,int target)
{
if(target == 0){
ret.push_back(tmp);
return;
}
for(int i = start+1 ; i < candidates.size() ; i++)//从start+1开始查找,避免了数字重复查找
{
if(candidates[i] < target){
tmp.push_back(candidates[i]);
combinationDfs(candidates,tmp,i,target-candidates[i]);
tmp.pop_back(); //回溯
}
else if(candidates[i] == target){
tmp.push_back(candidates[i]);
ret.push_back(tmp);
tmp.pop_back();//回溯
}
else
{
return;
}
while(i+1< candidates.size()&&candidates[i]==candidates[i+1]) i++;//去除重复的查找
}
}
};
【一天一道LeetCode】#40. Combination Sum II的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [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 40. Combination Sum 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 ...
- leetcode 40 Combination Sum II --- java
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...
- Java [Leetcode 40]Combination Sum II
题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
随机推荐
- post插件
分享牛系列,分享牛专栏,分享牛.在项目开发中,http请求方式是最常见的了.怎么模拟http请求呢?方法有很多种,可以使用httpclient直接模拟请求,也可以使用火狐post插件方式,这个章节主要 ...
- EBS开发之环境迁移
(一)环境迁移说明 1.1 迁移 由于EBS系统开发复杂,一般项目实施都是使用三套或者三套以上的系统,一套作为开发使用系统,一套作为集成测试系统,一套就是企业用的正式环境系统,在项目实施过程中对一 ...
- 深入浅出如何解析xml文件---上篇
xml小伙伴们并不陌生,xml是可扩展标记语言,标准通用标记语言语言的子集,是一种用来标记电子文件使其具有结构性的标记语言.我们知道xml可以用dom与sax等方法进行解析,但是xml为什么要解析呢? ...
- OpenCV:Mat元素访问方法、性能、代码复杂度以及安全性分析
欢迎转载,尊重原创,所以转载请注明出处: http://blog.csdn.net/bendanban/article/details/30527785 本文讲述了OpenCV中几种访问矩阵元素的方法 ...
- Ant简介
Ant,apache开源项目,基于Java的构建工具,是一个小程序.它通过自动完成所有的编译代码,运行测试以及 打包重新部署等繁琐费力的任务来帮助软件团队开发大程序: Ant的目标是自动完成所有的构建 ...
- 【移动开发】Service类onStartCommand()返回值和参数
Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...
- Android必知必会-Stetho调试工具
一.背景 Stetho是 Facebook 出品的一个强大的 Android 调试工具,使用该工具你可以在 Chrome Developer Tools查看APP的布局, 网络请求(仅限使用Volle ...
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 06 Activity隐式跳转
一,隐式跳转 某个Activity 需要在清单文件配置某个Activity如下信息: 注意:category要和action一起用 action:制定一个活动 在隐式跳转的可以用到 category ...
- SQL性能优化应该考虑哪些?
1.调整数据结构的设计.这一部分在开发信息系统之前完成,程序员需要考虑是否使用ORACLE数据库的分区功能,对于经常访问的数据库表是否需要建立索引等. 2.调整应用程序结构设计.这一部分也是在开 ...