【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 皇后不同的解决方案的数量. 示例: ...
随机推荐
- AMScrollingNavbar框架(自动隐藏导航栏)使用简介
AMScrollingNavbar框架是一个可以上拉隐藏导航栏和下拉显示导航栏的框架,这个开源框架的调用也很简单,本章节就给大家介绍一下这个框架的用法. 一.下载及导入框架 AMScrollingNa ...
- Centos 6/ 7下通过yum安装php7环境
本文转自:云溪社区 2015年12月初PHP7正式版发布,迎来自2004年以来最大的版本更新.PHP7最显著的变化就是性能的极大提升,已接近Facebook开发的PHP执行引擎HHVM.在WordPr ...
- SQL Server需要监控哪些计数器 ---指尖流淌
http://www.cnblogs.com/zhijianliutang/p/4174697.html
- 使用代码配置 NHibernate
多数情况下 NHibernate 使用配置文件进行配置, 但是我们也可以使用代码进行配置, 步骤如下: 1. 创建一个 Configuration using Nhibernate.cfg; var ...
- Integrate SharePoint 2013 with Team Foundation Server 2012
Now that SharePoint 2013 is out I want to make sure that I can integrate SharePoint 2013 with Team F ...
- crtmpserver配置文件详解
Configuration file配置文件 The configuration file is actually a lua script which must contain an object ...
- GB2312简体中文编码表
GB2312简体中文编码表 code +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F A1A0 . . · ˉ ˇ ¨ " 々 — - | … ...
- 【JSP EL】EL表达式里日期按照格式显示
转:http://blog.csdn.net/kaishuaige/article/details/8505174 JSP页面用EL表达式 输出date格式 1.头上引入标签 <%@ t ...
- 2009年末最强梅麻呂3D动画游戏大作 汉化补丁
[游戏名称]:Yin荡游戯Ω(前编)-闇の眷族vs女ドラゴン- (名字请自己补) [游戏厂商]:梅麻吕3D [发售日期]:2010年04月16日 游戏评价: 梅麻呂的3D作品能够说是如今3D动画中最好 ...
- Andorid之Annotation框架初使用(七)
Save Instance State:程序保留Activity的实例状态 , 在onSaveInstanceState(Bundle)被系统调用的时候自动保存 , onCreate(Bundle)被 ...