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. 参加SAP VT项目有感

    凡事预则立,不预则废. 没有接到录取电话还是有些悲伤的,虽然知道最终被录取的可能性不大,但是之前还是抱着一丝期望的,毕竟是自己的处女面,就这么以失败的结果结束了. 从最开始的投递简历,到后来的电话面试 ...

  2. js判断移动设备

    在开发中可能需要去判断用户的设备重定向到相应的网址: 1. 判断 iPhone  Android  iPod if((navigator.userAgent.match(/iPhone/i))||(n ...

  3. 断点续传队列和本地持久化(iOS源码)

    // // ASIFormDataRequest.m // Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest // ...

  4. Bipolar transistor boosts switcher's current by 12 times

    The circuit in Figure 1 uses a minimal number of external parts to raise the maximum output current ...

  5. SQLSERVER里面RR隔离级别没有GAP锁

    http://www.cnblogs.com/MYSQLZOUQI/articles/3862721.html

  6. 狗日的rem

    rem这是个低调的css单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃用了.但是我对rem综合评价是用来做web app它绝对是最合适的人选之一. ...

  7. Can't deserialize with binaryFormatter after changing namespace of class

    After changing the namespace of my class I can no longer deserialize the objects. I've implemented S ...

  8. BusyBox 简化嵌入式 Linux 系统

    BusyBox 是很多标准 Linux® 工具的一个单个可执行实现.BusyBox 包含了一些简单的工具,例如 cat 和 echo,还包含了一些更大.更复杂的工具,例如 grep.find.moun ...

  9. Ant:Ant 入门

    背景 自从有了 Maven 以后,Ant 视乎就不流行了,不过 Ant 还是有其应用场景的,Ant 的思想比较简洁,如下: 一个 project 包含多个 target(类似成员方法). 一个 tar ...

  10. u3d移动游戏优化规范

    1.顶点性能一般来说,如果您想在iPhone 3GS或更新的设备上每帧渲染不超过40,000可见点,那么对于一些配备 MBX GPU的旧设备(比如,原始的 iPhone,如 iPhone 3g和 iP ...