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) ☆的更多相关文章

  1. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  2. 【LeetCode】Combination Sum II(组合总和 II)

    这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...

  3. 【Leetcode】【Medium】Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  5. 【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 ...

  6. 【leetcode】Combination Sum

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  7. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. 【leetcode】Combination Sum (middle)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. 【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 ...

随机推荐

  1. DOS批处理命令-if语句

    IF语句是批处理中执行的条件分歧处理. 批处理中,IF分歧的写法有好几种,接下来,我们来一个一个的分析IF语法的结构. 1.IF [NOT] ERRORLEVEL 番号 批处理命令 当ERRORLEV ...

  2. GPU总结(1)

    目录 1.为什么要引入GPU 2.CUDA环境的搭建--CUDA全称Compute Unified Device Architecture统一计算设备架构,CUDA是以后总将GPU作为数据并行计算设备 ...

  3. Linux命令(4):cat命令

    1.作用:连接并显示指定的一个和多个文件的有关信息: 2.格式:cat [选项] 文件1 文件2 ... 其中文件1.文件2为要显示的多个文件: 3.常见参数: 4.使用实例: [youname@ww ...

  4. Web前端开发:SQL Jsp小项目(一)

    Jsp的学习算是告一段落,针对这段时间的学习,写了一个Jsp小项目来巩固学到的知识. 框架示意图 User list process UserAdd process 需要的界面效果: 需要工具:Ecl ...

  5. 在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能 (转载并优化处理篇)

    转自:http://blog.csdn.net/yanlele424/article/details/6895986 这段时间一直在做一个网站,其中遇到了一个问题,就是在服务器端压缩多个服务器端的文件 ...

  6. strcpy、strncpy、strlcpy的区别

  7. Item47

    STL迭代器分类:input迭代器.output迭代器.forward迭代器.bidirectional迭代器.random access迭代器. Input迭代器:只能向前移动,一次一步,客户只读取 ...

  8. Java数字格式化输出时前面补0

    Java数字格式化输出时前面补0 星期日 2014年11月30日|  分类: Java     /** * 里数字转字符串前面自动补0的实现. * */ public class TestString ...

  9. 韩顺平细说Servlet视频系列之tom相关内容

    韩顺平细说Servlet视频系列之tom相关内容 tomcat部署项目操作(注意:6.0版本以后的支持该操作,5.x版本需要另外配置?待验证!) 项目发布到tomcat的webapps文件下,然后启动 ...

  10. 【转载】GDB反向调试(Reverse Debugging)

    记得刚开始学C语言的时候,用vc的F10来调试程序,经常就是一阵狂按,然后一不小心按过了.结果又得从头再来,那时候我就问我的老师,能不能倒退回去几步.我的老师很遗憾地和我说,不行,开弓没有回头箭.这句 ...