【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 ...
随机推荐
- C# DateTime格式化
DateTime. ToString() -- :: ToBinary() - ToFileTime() ToFileTimeUtc() ToLocalTime() -- :: ToLongDateS ...
- 文件流操作(FileStream,StreamReader,StreamWriter)
大文件拷贝: /// <summary> /// 大文件拷贝 /// </summary> /// <param name="sSource"> ...
- 页面所有的button绑定同一个事件,点击不同的button赋值不同
<script type="text/javascript"> $(function(){ $("input[type='button']").cl ...
- 版本控制工具--GIT 基本命令(1)
一.安装GIT,在官网上下载安装即可(下面模拟环境是window7) 二.基本操作: 1.创建GIT库: ①先使用mkdir命令创建一个空目录,再使用git init将该目录变成GIT库,会在该目录下 ...
- OC2-重写
// // Dog.h // OC2-重写 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng. All rig ...
- Linux下Tomcat启动正常,但浏览器无法访问
1.服务器可ping通 2.服务器抓本地的http请求包,可以抓到 3.本地抓服务器返回的http响应包,抓不到 经过查找,是由于开启了Linux防火墙 查看防火墙配置(需要root权限) [root ...
- Mysql 的安装与配置
MySQL的安装 第1步:下载 第2 步:以管理员身份进行安装 第3步:选择安装类型. 第4步:设置MySQL安装目录,及数据库文件目录 第5步:安装结束,开启配置向导 第6步:选择配置类型 第7步: ...
- poj 2220 Sumsets
Sum ...
- Ng机器学习笔记-1-一元线性回归
一:回归模型介绍 从理论上讲,回归模型即用已知的数据变量来预测另外一个数据变量,已知的数据属性称为输入或者已有特征,想要预测的数据称为输出或者目标变量. 下图是一个例子: 图中是某地区的面积大小与房价 ...
- L013-oldboy-mysql-dba-lesson13
L013-oldboy-mysql-dba-lesson13 02 18:00 来自为知笔记(Wiz)