【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 ...
随机推荐
- Linux/centos/redhat下各种压缩解压缩方式详解
1.zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip -o -d ...
- 【leetcode】363. Max Sum of Rectangle No Larger Than K
题目描述: Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the ma ...
- mysql 导出表结构
mysql导出数据库各表结构,很简单只要一条命令即可: mysqldump -uxxx -d databasename [,table] > xxx.sql mysqldump中-d参数即为只导 ...
- 在Apache中开启虚拟主机
最近在自学LAMP,在Apache中尝试着开启虚拟主机的时候,遇到了挺多麻烦的,这里也顺便总结一下,在Apache中开启虚拟主机的时候,主要有下面几个步骤: 1.新建一个文件夹作为虚拟主机,用来存储网 ...
- c# Aes加解密和对象序列化
aes加解密 public class AesCryptto { private string key = "hjyf57468jhmuist"; private string i ...
- mapper配置
一:查询 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC &q ...
- 将XML文件保存到DataGridView中
#region get护理单记录信息XML //将XML文件保存到DataTable private DataTable FromXML2DataTable(string XMLStr,string ...
- CocoaPods安装和使用及问题:Setting up CocoaPods master repo
CocoaPods是什么? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而 ...
- [译]Java Thread wait, notify和notifyAll示例
Java Thread wait, notify和notifyAll示例 Java上的Object类定义了三个final方法用于不同线程间关于某资源上的锁状态交互,这三个方法是:wait(), not ...
- php入门单引号与双引号区别
[1]单引号和双引号在处理变量的时候做法: 括在双引号内的变量会解释出值,但是括在单引号内则不做处理,直接输出: <?php $var = 'my name is huige'; echo &q ...