【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 ...
随机推荐
- Java-transient
transient的作用及使用方法 都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过程,只要这 ...
- 【HDU 5438】Ponds
题意 不断删去度数为1的点,最后求有奇数个点的联通块的权值之和. 分析 存边的时候,要头尾都存这个边.用dfs或者队列删点,再用并查集或者dfs确定联通块,然后统计联通块的点数,最后累加. 我自己写的 ...
- python多态
多态是面向对象语言的一个基本特性,多态意味着变量并不知道引用的对象是什么,根据引用对象的不同表现不同的行为方式.在处理多态对象时,只需要关注它的接口即可,python中并不需要显示的编写(像Java一 ...
- 21.Android之SQLite数据库学习
Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLite 只需要带一个动 ...
- BZOJ1208 宠物收养所
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- TYVJ P1403 [NOIP2010]关押罪犯
TYVJ的编译器总是要搞点岔子出来,上次是double必须用f输出而不能用lf,这次又不知道为何CE 于是去了洛谷P1525测试,AC 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1 ...
- Intrusion Analysis Learning
目录 . 入侵分析简介 . 基于日志的入侵分析技术 . 入侵分析CASE . 入侵分析CASE . 入侵分析CASE . 入侵分析CASE 1. 入侵分析简介 Windows 清除日志的方法 wmic ...
- PL/0编译器(java version)–PL0.java
1: package compiler; 2: 3: import java.io.BufferedWriter; 4: import java.io.FileWriter; 5: 6: /* ...
- Android Studio集成SVN报错:can't use subversion command line client : svn
Android Studio集成SVN插件,check out出代码后,每次开启都会在右上角出现如下错误: Can't use Subversion command line client: svn ...
- POI读写Excel简述之读取
一.POI读取Excel文件(以Excel2003版为例,2007版就是根据文件扩展名xlsx将HSSFWorkbook换为XSSFWorkbook,及其Sheet.Row.Cell也相应替换) // ...