【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 ...
随机推荐
- JS模式:又一个简单的工厂模式
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- C语言浮点数除法可以精确到多少位小数
double型的两个数相除,得到的浮点数能精确到多少位呢..用我家电脑做了个实验,编译器是Code::Blocks 13.12. 然后用电脑自带的计算器算的结果和C语言算的结果比较如图. 第一例里a= ...
- Teradata(不同date输出要求;表类型)
1. 需要某种特定形式的date 类型export 到文件中,例如 YYYYMMDD/ YYYY-MM-DD 这时候不一定非要用date 类型,可以转换为varchar 类型! CAST(CAST ( ...
- 最小圆覆盖(Smallest Enclosing Discs)
随机增量算法(a randomized incremental algorithm) #define sqr(x) ((x)*(x)) #define EPS 1e-4 struct P{ doubl ...
- Google Protocol Buffer 的使用和原理
Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...
- 在64位windows 7上安装汇编调试工具debug.exe的方法
最近我在研究汇编,书中介绍的调试工具还是基于WinXP 32bit时代中自带debug.exe进行调试,但是64bit的Windows XP.Vista.Win7.Win8都已经不自带这个工具了,网上 ...
- 漂亮的title提示信息
<HTML> <HEAD> <title>一种很酷的文字提示效果演示</title> <style> .tableBorder7{width ...
- WinFrom 只启动一个exe,并且获得焦点
只启动一个exe方法: using System; using System.Collections.Generic; using System.Runtime.InteropServices; us ...
- LinkedBlockingQueue和ConcurrentLinkedQueue详细用法
1.LinkedBlockingQueue<E>:java.util.concurrent API中的解释: public class LinkedBlockingQueue<E&g ...
- git基础知识总结
1,clone git clone https://github.com/KoMiles/helloword helloword 2,pull git pull 3,commit git commit ...