【leetcode】Subsets II
Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int> > result;
vector<int> tmp;
sort(S.begin(),S.end());
getSubset(result,S,,tmp);
return result;
}
void getSubset(vector<vector<int> > &result,vector<int> &S,int index,vector<int> tmp)
{
if(index==S.size())
{
for(int i=;i<result.size();i++)
{
if(result[i]==tmp)
return;
}
result.push_back(tmp);
return;
}
getSubset(result,S,index+,tmp);
tmp.push_back(S[index]);
getSubset(result,S,index+,tmp);
}
};
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int> > result;
vector<int> tmp;
sort(S.begin(),S.end());
getSubset(result,S,,tmp);
return result;
}
void getSubset(vector<vector<int> > &result,vector<int> &S,int index,vector<int> tmp)
{
result.push_back(tmp);
for(int i=index;i<S.size();i++)
{
if(i>index&&S[i]==S[i-])continue;
tmp.push_back(S[i]);
getSubset(result,S,i+,tmp);
tmp.pop_back();
}
}
};
【leetcode】Subsets II的更多相关文章
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】课程表 II
[问题]现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程.例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及 ...
- 【Leetcode】【Medium】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【leetcode】N-Queens II
N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...
- 【leetcode】Subsets
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 【LeetCode】 Subsets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
随机推荐
- BIEE 创建一个简单的分析(2)
步骤: 1.如果BIEE安装在本机,直接登录http://localhost:9704/analytics/ 点击右上方导航菜单中的“新建->分析” 2.选择上节创建的RPD文件中的SCOTT主 ...
- POJ-2777Count Color 线段树+位移
这道题对于我这样的初学者还是有点难度的不过2遍A了还是很开心,下面说说想法-- Count Color Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- SQL Server数据库还原:"因为数据库正在使用,所以无法获得对数据库的独占访问权"
如题,网上找了一些客套的方法,如果不想去折腾,请看我的方法: 1.先脱机数据库,这个目的就是为了停掉所有链接 2.选择还原数据库,如果提示日志尾部不完整,请选择数据库属性的选项,覆盖现有数据. 还可以 ...
- 洛谷P2925 [USACO08DEC]干草出售Hay For Sale
题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...
- Rootkit Hunter Sourcecode Learning
目录 . Rootkit Hunter Introduce() . Source Code Frame() . do_system_check_initialisation() . do_system ...
- DEDECMS数据库执行原理、CMS代码层SQL注入防御思路
我们在上一篇文章中学习了DEDECMS的模板标签.模板解析原理,以及通过对模板核心类的Hook Patch来对模板的解析流量的攻击模式检测,达到修复模板类代码执行漏洞的目的 http://www.cn ...
- Maven学习笔记-03-Eclipse下maven项目在Tomcat7和Jetty6中部署调试
现在最新的Eclipse Luna Release 已经内置了Maven插件,这让我们的工作简洁了不少,只要把项目直接导入就可以,不用考虑插件什么的问题,但是导入之后的项目既可以部署在Tomcat也可 ...
- linux桌面应用开发之折腾
1 起因 需要开发一个wifi定位的应用,最先在android下搞,后来因为多网卡的原因要换平台,经历了windows,最终选择用kaili linux.debian系的linux,开发桌面应用怎么办 ...
- 位图索引:原理(BitMap index)
http://www.cnblogs.com/LBSer/p/3322630.html 位图(BitMap)索引 前段时间听同事分享,偶尔讲起Oracle数据库的位图索引,顿时大感兴趣.说来惭愧,在这 ...
- hdu 2084 数塔(动态规划)
本题是一个经典的动态规划题. 直接利用记忆化搜索:见图解 Ac code : #include<stdio.h> #include<string.h> #define max( ...