【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 皇后不同的解决方案的数量. 示例: ...
随机推荐
- PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- 利用BusyBox ~私人定制 My LINUX~
前言 我在今天在这里跟大家详细地探讨一下Linux系统的定制过程和实现例如.用户能够远程登录:和Nginx能够稳定地运行在我们私人定制的LINUX系统上.一步一步从头开始定制属于我们自己的系统. 正文 ...
- boost.python编译及演示样例
欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46781581 linux编译boost的链接:http://bl ...
- MRIcro tutorial -- mricro 教程
MRIcro tutorial 参考网址:http://www.mccauslandcenter.sc.edu/mricro/mricron/ http://www.cabiatl.com/mri ...
- github pages+hexo自建博客
1.github创建新项目,然后开启pages即可 2.全局安装hexo npm install -g hexo 3.初始化hexo hexo init 4.安装hexo的依赖 npm i 5.基本上 ...
- 描述 Machine.Config 和 Web.Config(转载)
NET Framework 提供的配置管理包括范围广泛的设置,允许管理员管理 Web 应用程序及其环境.这些设置存储在 XML 配置文件中,其中一些控制计算机范围的设置,另一些控制应用程序特定的配置. ...
- LTE试题
D 如果出现eNB的告警1018007“小区退服,光口不可用”,不可能是以下哪种原因造成的?( ) 基带板上Ir接口光模块损坏 基带板上Ir接口光模块被拔出 基带板上Ir接口光模块型 ...
- matlab从文件夹名中获得该文件夹下所图像文件名
function [s,nameC]=get_FileNameFromFolderPath(path) % 函数调用:[s,nameC]=get_FileNameFromFolderPath(path ...
- 排查VMWare虚拟机的性能问题
Troubleshooting ESX/ESXi virtual machine performance issues (2001003) http://kb.vmware.com/selfservi ...
- mssql Sqlver 修改标识列方法
摘自: http://www.cnblogs.com/badboy2008/articles/1145465.html MSSQL Server修改标识列方法 ----允许对系统表进行更新exec ...