【LeetCode】90. Subsets II (2 solutions)
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],
[]
]
解法一:
举例说明我的算法:
设S={1,2,2},则size=3
在不考虑重复的情况下,子集共有2^3=8个
分别为:
【1,2,2】
0,0,0 {}
0,0,1 {2}
0,1,0 {2}
0,1,1 {2,2}
1,0,0 {1}
1,0,1 {1,2}
1,1,0 {1,2}
1,1,1 {1,2,2}
1表示对应位的元素存在于集合中,0表示不存在。
因此只要从0遍历到2^size - 1,如果对应位为1,则将S中相应元素加入当前集合。
对于Note的两点要求:
1、sort函数对集合排序
2、使用map去重,存在即去除。
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int> > result;
map<vector<int>, bool> m;
int size = S.size();
for(int i = ; i < pow(2.0, size); i ++)
{
int tag = i;
vector<int> cur;
for(int j = size-; j >= ; j --)
{
if(!tag)
break;
if(tag% == )
{
cur.push_back(S[j]);
}
tag >>= ;
}
sort(cur.begin(), cur.end());
if(m.find(cur) == m.end())
{
m[cur] = true;
result.push_back(cur);
}
}
return result;
}
};

解法一额外开辟了map,因此占用了大量的空间。
可以这样来看。
后加入的元素,需要加入全部已有的集合,并且考虑重复。
再次考虑S={1,2,2},先排序。
首先加入空集{}
对于元素1,需要加入{},成为新的集合{1}
对于元素2,需要加入{}和{1},成为新的集合{2}和{1,2}。考虑重复,再产生新集合{2,2}和{1,2,2}。
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int> > ret;
vector<int> empty;
ret.push_back(empty);
sort(S.begin(), S.end());
unordered_map<int, int> count;
for(int i = ; i < S.size(); i ++)
count[S[i]] ++;
vector<int>::iterator iter = unique(S.begin(), S.end());
S.erase(iter, S.end());
for(int i = ; i < S.size(); i ++)
{
int size = ret.size();
for(int j = ; j < size; j ++)
{
vector<int> newset = ret[j];
for(int k = ; k < count[S[i]]; k ++)
{
newset.push_back(S[i]);
ret.push_back(newset);
}
}
}
return ret;
}
};

【LeetCode】90. Subsets II (2 solutions)的更多相关文章
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
- 【一天一道LeetCode】#90. Subsets II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】78. Subsets (2 solutions)
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 【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】基本计算器II
[问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 .整数除法仅保留整数部分. 输入: "3+2*2" ...
- 【LeetCode 】N皇后II
[问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...
随机推荐
- Get buck-boost performance from a boost regulator
The SEPIC (single-ended, primary-inductance-converter) topology is generally a good choice for volta ...
- FreeRTOS Memory Management ( IAR )
http://www.freertos.org/a00111.html The RTOS kernel allocates RAM each time a task, queue, mutex, so ...
- 在WPF中显示动态GIF
在我们寻求帮助的时候,最不愿意听到的答复是:很抱歉,在当前版本的产品中还没有实现该功能... 在WPF中显示动态的GIF图像时便遇到了这样的问题,WPF中强大的Image控件却不支持动态的GIF(其只 ...
- 在 Linux 系统中安装Load Generator ,并在windows 调用
原文地址:http://www.blogjava.net/qileilove/archive/2012/03/14/371861.html 由于公司需要测试系统的最大用户承受能力,所以需要学习使用lo ...
- Python continue 语句
Python continue 语句 Python continue 语句跳出本次循环,而break跳出整个循环. continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮 ...
- Spring Data JPA -1-CRUD入门
1) 引入jar包支持 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- 如何使用Omnifocus做时间管理 4 多项目管理
这一篇和大家分享作为一个管理者,可能遇到的时间管理挑战和如何用omnifocus应对这些挑战. 张经理在一家IT公司上班,从程序员开始打拼了5年,凭借权威的技术能力当上了项目经理,同时管理三个项目,苦 ...
- 第六篇 ANDROID窗口系统机制之显示机制
第六篇 ANDROID窗口系统机制之显示机制 ANDROID的显示系统是整个框架中最复杂的系统之一,涉及包括窗口管理服务.VIEW视图系统.SurfaceFlinger本地服务.硬件加速等.窗口管理服 ...
- MyEclipse for Linux版下载
最近看到很多网友都在找MyEclipse for Linux版下载,费了很大劲也没有找到.1.建议通过代理到官方网站下载. 2.用迅雷下载.设置迅雷使用代理下载(我用的就是这种方式). MyEclip ...
- javascript学习笔记------概念相关
javascript中的函数.对象 1. 在javascript中,函数是被当成一种数据类型,它可以被存储在一个变量.数组.对象中,可以被当作参数传递到另一个函数中. 函数就像是字符串和数字这样的的数 ...