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)的更多相关文章

  1. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  2. 【LeetCode】90.Subsets II

    Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...

  3. 【一天一道LeetCode】#90. Subsets II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. LeetCode Problem 90. Subsets II

    python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...

  5. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  6. 【LeetCode】78. Subsets (2 solutions)

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  7. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  8. 【LeetCode】基本计算器II

    [问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格  .整数除法仅保留整数部分. 输入: "3+2*2" ...

  9. 【LeetCode 】N皇后II

    [问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...

随机推荐

  1. tomcat使用Eclipse进行远程调试(线上调试)

    什么是远程调试,就是在A机器上利用Eclipse单步跟踪调试B机器上的Web应用,当然调试A机器上Web应用也是没有问题的,90%我都是调试本机的Web应用,远程调试的意义我想我不用说了,大家都会想到 ...

  2. jquery 常用获取值得方法汇总

    jquery取radio单选按钮的值$("input[name='items']:checked").val();jquery radio取值,checkbox取值,select取 ...

  3. iOS开源项目大全

    UI界面类项目: Panoramagl —— 720全景展示 Panorama viewer library for iPhone, iPad and iPod touch MBProgressHUD ...

  4. MatLab角点检測(harris经典程序)

    http://blog.csdn.net/makenothing/article/details/12884331 这是源博客的出处,鄙人转过来是为了更好的保存!供大家一起学习!已将原始的博客的文章的 ...

  5. 升压转换器 (Boost)

    升压转换器 (Boost) 需要将输入电压转换为较高的输出电压时,升压转换器 (Boost)是唯一的选择. 升压转换器透过内部 MOSFET 对电压充电来达成升压输出的目的,而当 MOSFET 关闭时 ...

  6. XY8782S00 BL-W8782 BL-R8782MS1 SDIO接口 高性能、低功耗、体积小 wifi无线模块

    1.产品简介 BL-8782是一款高性能.低功耗.体积小SDIO接口无线模组,符合IEEE802.11N标准,并向下兼容IEEE802.11B/G标准,支持IEEE 802.11i安全协议,以及IEE ...

  7. Ubuntu OS应用Runtime Enviroment

    在这篇文章中.我们将介绍Ubuntu OS的Runtime Environment.在文章"App confinement: Security policy for click packag ...

  8. java 中 BigDecimal 怎么与 0 比较

    标准做法int r=big_decimal.compareTo(BigDecimal.Zero); //和0,Zero比较if(r==0) //等于if(r==1) //大于if(r==-1) //小 ...

  9. 判断浏览器内核JS代码

    <script type="text/javascript"> var Sys = {}; var ua = navigator.userAgent.toLowerCa ...

  10. 《Haskell趣学指南》

    <Haskell趣学指南> 基本信息 原书名:Learn You a Haskell for Great Good!: A Beginner's Guide 原出版社: No Starch ...