【leetcode】Combination Sum II (middle) ☆
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums toT.
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]
思路:
把重复的数字看做一个整体,只能出现0-重复次数遍。 这个代码特别慢,不知道为什么 550ms
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
vector<vector<int>> ans;
if(num.empty())
return ans;
sort(num.begin(), num.end()); //从小到大排序
recursion(ans, num, , target);
return ans;
}
void recursion( vector<vector<int> > &ans, vector<int> candidates, int k, int target)
{
static vector<int> partans;
if(target == ) //如果partans中数字的总和已经达到目标, 压入答案
{
ans.push_back(partans);
return;
}
if(k >= candidates.size() || target < )
return;
int num = candidates[k];
int copy = ;
while(k < candidates.size() && candidates[k] == num)
{
k++;
copy++;
}
recursion(ans, candidates, k, target); //不压入当前数字
for(int i = ; i <= copy; i++)
{
partans.push_back(num); //压入当前数字
recursion(ans, candidates, k , target - i * num); //后面只压入大于当前数字的数,避免重复
}
//恢复数据
while(!partans.empty() && partans.back() == num)
{
partans.pop_back();
}
}
};
大神的13ms代码,感觉和我的差距不大,为什么速度快这么多。
class Solution {
vector <int> path;
vector < vector <int> > res;
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
sort(num.begin(), num.end());
gen(, target, num);
return res;
}
void gen(int index, int sum, vector <int> &nums) {
if (sum == ) {
res.push_back(path);
return;
}
for (int i = index; i < nums.size(); i++) { //只压入序号大于等于i的数字 避免重复
if (sum - nums[i] < ) return;
if (i && nums[i] == nums[i - ] && index < i) continue; //每次递归相同的数字只压入一次
path.push_back(nums[i]); //这里不需要不压入nums[i]的情况,因为循环到后面时自然就是未压入该数的情况了
gen(i + , sum - nums[i], nums);
path.pop_back();
}
}
};
【leetcode】Combination Sum II (middle) ☆的更多相关文章
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【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 ...
随机推荐
- javascript中常用的一些功能及正则表达式的用法
一.取得地址栏后的参数 /** * 假设地址栏url为:login.do?username = "这里中文的话会是特殊字符组成的" */ //定义一个取得参数值的函数 functi ...
- poi-3.11-beta2-20140822.jar操作excel方法
poi-3.11-beta2-20140822.jar操作excel方法 根据不同类型读取值的方法: // 获取单元格内不同类型的值 public String getValueByType(HSSF ...
- 20141015--for语句2
for语句,打印等腰三角形: 第一种方法:(使用for语句嵌套) 第二种方法:(定义string型变量) 以下是其他形状的等腰三角形: (穿插使用了for语句嵌套,定义string型)
- 设置 MyEclipse 默认打开文件方式
在web开发时,我们在打开JSP页面时,MyEclipse默认使用 Vistual 的方式打开.这种方式打开JSP页面时,很慢.在实际的开发过程中我们不想使用这种默认的方式打开JSP.设置如 ...
- vs2008+qt进行开发
第一次接触qt vs,完全小白,网上找资料,各种乱,还大部分是很早以前的,挣扎了好几天终于搞定了, 在此给大家分享. 首先是下载vs2008 (我的项目组项目要求用这个版本),这个比较容易下载:然后是 ...
- C#中调用unmanaged DLL
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 按照自己的理解实现比特交换协议(alternating-bit protocol)
一开始的思路是想写两个程序的,发送端和接收端.但是后来想了下,因为是模拟的,所以不用这么麻烦,直接自己定制场景(比如说丢包阿,包出错阿,超时之类的). 基本上是根据上图所写的,一个函数发包,一个函数接 ...
- html5 的<audio> 音频 audio的“坑”
<audio>标签是html5的一个非常有意义的特性.告别的flash的时代.它的属性有: autoplay:音频就绪后马上播放 controls:出现该属性,向用户显示播放的控件. lo ...
- python字典根据value排序
参考: http://docs.python.org/2/howto/sorting.html http://www.cnpythoner.com/post/266.html http://ghost ...
- Discuz 哪些文件和文件夹需要777权限
Discuz有强大的缓存能力,当然要想建立这些缓存,就需要对需要的文件和文件夹进行读写,因此给需要的文件和文件夹777权限(Linux服务器)是必不可少的,下面就列出了需要给777权限的文件和文件夹, ...