【leetcode】Combination Sum II
Combination Sum II
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)
{
sort(candidates.begin(),candidates.end());
vector<vector<int> > result;
vector<int> tmp;
backtracking(result,candidates,target,tmp,,);
return result;
}
void backtracking(vector<vector<int> > &result,vector<int> &candidates, int &target,vector<int> tmp, int sum,int index)
{
if(sum==target)
{
result.push_back(tmp);
return;
}
else
{
int sum0=sum;
for(int i=index;i<candidates.size();i++)
{
if(i>index&&candidates[i]==candidates[i-])
{
continue;
}
tmp.push_back(candidates[i]);
sum=sum0+candidates[i];
if(sum>target)
{
break;
}
backtracking(result,candidates,target,tmp,sum,i+);
tmp.pop_back();
}
}
}
【leetcode】Combination Sum II的更多相关文章
- 【leetcode】Combination Sum II (middle) ☆
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】【Medium】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【leetcode】Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Combination Sum (middle)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode】Path Sum II 二叉树递归
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- [转] 再探java基础——break和continue的用法
原文地址:http://blog.csdn.net/luoweifu/article/details/10756017 break break可用于循环和switch...case...语句中. 用于 ...
- 39.Android版本小知识
中文名----英文名----版本----对应API Level 棉花糖 Marshmallow - 6.0.1_r10 - API 23棉花糖 Marshmallow - 6.0.0_r5 - API ...
- BZOJ 3110 [Zjoi2013]K大数查询
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...
- Dancing Links初学记
记得原来备战OI的时候,WCX大神就研究过Dancing Links算法并写了一篇blog.后来我还写了个搜索策略的小文章( http://www.cnblogs.com/pdev/p/3952279 ...
- Android APK反编译问题
因为这几天想做一个离线音乐播放器,然后需要很多.png图片,在度娘上搜图片感觉效果不好, 就需要用到反编译工具来将一些.apk文件中的资源文件提取出来. 这里就只讲下怎么使用工具来进行反编译,什么?你 ...
- ActivityInfo taskAffinity
通常在Manifest里面使用 <application android:icon="@drawable/icon" android:label="@string/ ...
- ubuntu下安装 openssl 开发库
ubuntu下安装 openssl 开发库 检查是否已安装openssl: sudo apt-get install openssl 如果已安装执行以下操作:sudo apt-get install ...
- 极大似然估计、贝叶斯估计、EM算法
参考文献:http://blog.csdn.net/zouxy09/article/details/8537620 极大似然估计 已知样本满足某种概率分布,但是其中具体的参数不清楚,极大似然估计估计就 ...
- C文件操作(全)
引用自:http://www.cnblogs.com/whiteyun/archive/2009/08/08/1541822.html 文件 文件的基本概念 所谓“文件”是指一组相关数据的有序集合. ...
- 32和64位的CentOS 6.0下 安装 Mono 2.10.8 和Jexus 5.0
http://www.cnblogs.com/shanyou/archive/2012/01/07/2315982.html shanyou 博客