【Combination Sum II 】cpp
题目:
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]
代码:
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int>& candidates, int target)
{
vector<vector<int> > ret;
std::sort(candidates.begin(), candidates.end());
int sum = ;
vector<int> tmp;
Solution::dfs(ret, tmp, sum, candidates, , candidates.size()-, target);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& tmp,
int &sum,
vector<int>& candidates,
int begin,
int end,
int target
)
{
if ( sum>target ) return;
if ( sum==target )
{
ret.push_back(tmp);
return;
}
int pre = candidates[]-;
for ( int i=begin; i<=end; ++i )
{
if ( pre==candidates[i] ) continue;
pre = candidates[i];
if ( sum+candidates[i]<=target )
{
sum += candidates[i];
tmp.push_back(candidates[i]);
Solution::dfs(ret, tmp, sum, candidates, i+, end, target);
tmp.pop_back();
sum -= candidates[i];
}
}
}
};
tips:
此题与combination sum不同之处在于,每个元素只能取一次,并且解集中不能有重复的。
用深搜模板:
1. 如果元素都没有重复的,就是最直接的深搜模板(注意dfs到下一层的时候,传入的begin是i+1而不是i了)
2. 如果元素有重复的,再处理时就跳过前面出现过的元素(前提是candidates都排好序);这里有个技巧就是维护一个pre变量,并且初始化pre为candidates[0]-1,即比candidates元素都小,这样不用改变循环的结构就可以直接处理
3. 还有一个疑问,为什么不用判断begin>end的情况?比如,{1,1,1} ,4 这种输入,显然所有元素加一起也满足不了结果。进行到最后一定会出现begin==3 end==2的情况。这种情况也不要紧,因为begin大于end就不处理了,直接返回了,所以也没事。
===========================================
第二次过这道题,遇到这种不要重复结果的,有些规律:就是在每一层dfs的时候,如果candidate[i]出现连续两个重复,就跳过后面的那个。
class Solution {
public:
vector<vector<int> > combinationSum2(
vector<int>& candidates, int target)
{
sort(candidates.begin(), candidates.end());
vector<vector<int> > ret;
vector<int> tmp;
Solution::dfs(ret, tmp, candidates, , candidates.size()-, target);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& tmp,
vector<int>& candidates,
int begin,
int end,
int target
)
{
if ( target< ) return;
if ( target== )
{
ret.push_back(tmp);
return;
}
if ( begin>end ) return;
int pre = candidates[begin]-;
for ( int i=begin; i<=end; ++i )
{ if ( pre==candidates[i]) continue;
pre = candidates[i];
tmp.push_back(candidates[i]);
Solution::dfs(ret, tmp, candidates, i+, end, target-candidates[i]);
tmp.pop_back();
}
}
};
【Combination Sum II 】cpp的更多相关文章
- 【Path Sum II】cpp
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- 【二叉树的递归】04找出二叉树中路径和等于给定值的所有路径【Path Sum II】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【Jump Game II 】cpp
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【Single Num II】cpp
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
随机推荐
- eCharts图表(polar极坐标图)
极坐标图 HTML: <div id="eChart"></div> css: #eChart{ width:500px; height:500px; } ...
- mathtype 章节号 Equation Chapter 1 Section 1 的去除
mathtype 章节号 Equation Chapter 1 Section 1 的去除 转:http://hi.baidu.com/17ximm/blog/item/2882413e92fc96c ...
- LAMP stack-5.6.22 (OpenLogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: apache2.4.20 mysql5.6.30 php5.6.22 apache application server basic softwar ...
- LeetCode Number of 1 Bits 计算1的个数
题意: 提供一个无符号32位整型uint32_t变量n,返回其二进制形式的1的个数. 思路: 考察二进制的特性,设有k个1,则复杂度为O(k).考虑将当前的数n和n-1做按位与,就会将n的最后一个1去 ...
- FPGA工具篇——编辑器Notepad++
body { font-family: 微软雅黑,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLi ...
- 机器学习-octave使用
1 == 2 % false 1 ~=2 % true % 隐藏版本,只显示>> . PS1('>> '); % 输出两位小数格式 disp(sprintf('2 ...
- Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)
对角线上的元素就是a[i],而且在所在行和列中最大, 首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序. 经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i] ...
- Finite Encyclopedia of Integer Sequences(找规律)
6617: Finite Encyclopedia of Integer Sequences 时间限制: 1 Sec 内存限制: 128 MB提交: 375 解决: 91[提交] [状态] [讨论 ...
- bootstrap table 自定义checkbox样式
//css <style> .checkbox-custom { position: relative; padding: 0 15px 0 25px; margin-bottom: 7p ...
- java编程基础——二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 题目代码 /** * @program: JavaCode * @description: 操作给定的二叉树,将其变换为源二叉树的镜像. * 二 ...