题目:

Given a list of numbers that may has duplicate numbers, return all possible subsets

Notice

  • Each element in a subset must be in non-descending order.
  • The ordering between two subsets is free.
  • The solution set must not contain duplicate subsets.

Example

If S = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

题解:

Solution 1 ()

class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> S) {
vector<vector<int> > res;
vector<int> v;
sort(S.begin(), S.end());
Dfs(S, res, v, ); return res;
} void Dfs(vector<int> S, vector<vector<int> > &res, vector<int> &v, int pos) {
res.push_back(v); for (int i = pos; i < S.size(); ++i) {
if (i == pos || S[i] != S[i - ]) {
v.push_back(S[i]);
Dfs(S, res, v, i + );
v.pop_back();
}
}
}
};

  To solve this problem, it is helpful to first think how many subsets are there. If there is no duplicate element, the answer is simply 2^n, where n is the number of elements. This is because you have two choices for each element, either putting it into the subset or not. So all subsets for this no-duplicate set can be easily constructed:

num of subset

  • (1 to 2^0) empty set is the first subset
  • (2^0+1 to 2^1) add the first element into subset from (1)
  • (2^1+1 to 2^2) add the second element into subset (1 to 2^1)
  • (2^2+1 to 2^3) add the third element into subset (1 to 2^2)
  • ....
  • (2^(n-1)+1 to 2^n) add the nth element into subset(1 to 2^(n-1))

Then how many subsets are there if there are duplicate elements? We can treat duplicate element as a spacial element. For example, if we have duplicate elements (5, 5), instead of treating them as two elements that are duplicate, we can treat it as one special element 5, but this element has more than two choices: you can either NOT put it into the subset, or put ONE 5 into the subset, or put TWO 5s into the subset. Therefore, we are given an array (a1, a2, a3, ..., an) with each of them appearing (k1, k2, k3, ..., kn) times, the number of subset is (k1+1)(k2+1)...(kn+1). We can easily see how to write down all the subsets similar to the approach above.

Solution 2 ()

class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int> > res{{}};
sort(S.begin(), S.end());
for (int i = ; i < S.size(); ) {
int cnt = ;
while (cnt + i < S.size() && S[cnt + i] == S[i]) {
++cnt;
}
int size = res.size();
for (int j = ; j < size; ++j) {
vector<int> instance = res[j];
for (int k = ; k < cnt; ++k) {
instance.push_back(S[i]);
res.push_back(instance);
}
}
i += cnt;
}
return res;
}
};

Solution 3 ()

class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int> > res{{}};
sort(S.begin(), S.end());
int size = ;
int last = !S.empty() ? S[] : ;
for (int i = ; i < S.size(); ++i) {
if (last != S[i]) {
last = S[i];
size = res.size();
}
int newsize = res.size();
for (int j = newsize - size; j < newsize; ++j) {
res.push_back(res[j]);
res.back().push_back(S[i]);
}
}
return res;
}
};

【Lintcode】018.Subsets II的更多相关文章

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

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

  2. 【LeetCode】90. Subsets II (2 solutions)

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

  3. 【LeetCode】90.Subsets II

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

  4. 【Lintcode】017.Subsets

    题目: 题解: Solution 1 () class Solution { public: vector<vector<int> > subsets(vector<in ...

  5. 【lintcode】二分法总结 II

    Half and Half 类型题 二分法的精髓在于判断目标值在前半区间还是后半区间,Half and Half类型难点在不能一次判断,可能需要一次以上的判断条件. Maximum Number in ...

  6. 【Lintcode】033.N-Queens II

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  7. 【动态规划】简单背包问题II

    问题 B: [动态规划]简单背包问题II 时间限制: 1 Sec  内存限制: 64 MB提交: 21  解决: 14[提交][状态][讨论版] 题目描述 张琪曼:“为什么背包一定要完全装满呢?尽可能 ...

  8. 【贪心】时空定位II

    [贪心]时空定位II 题目描述 有一块空间,横向长w,纵向长为h,在它的横向中心线上不同位置处装有n(n≤10000)个点状的定位装置,每个定位装置i定位的效果是让以它为中心半径为Ri的圆都被覆盖.请 ...

  9. 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)

    [UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 ...

随机推荐

  1. JavaScript -- JavaScript DOM 编程艺术(第2版)

    /* 渐进增强 平稳退化 网页 结构层(structural layer): HTML 表示层(presentation layer): CSS <link rel="styleshe ...

  2. IOS --支付宝SDK 分解讲解

    开发在手机端的时候(客户端),我们主要负责客户端(手机端)的开发,所以那些繁琐的到支付宝官网填写商户信息可以留给后台去弄,后台只要把: 1回调地址, 2app的ID, 3商户的私钥(privateKe ...

  3. maven scope runtime

    https://blog.csdn.net/ningbohezhijunbl/article/details/25818069 There are 6 scopes available: compil ...

  4. 安装anaconda及pytorch

    安装anaconda,下载64位版本安装https://www.anaconda.com/download/    官网比较慢,可到清华开源镜像站上下载 环境变量: D:\Anaconda3;D:\A ...

  5. Asp.net MVC3 异常全局过滤器处理

    1.建立异常全局过滤器处理机制,在Gloabal.asax.cs文件中,有如下代码块: public static void RegisterGlobalFilters(GlobalFilterCol ...

  6. jQuery学习笔记(8)--表格筛选

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  7. 九度OJ 1049:字符串去特定字符 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8499 解决:3860 题目描述: 输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果. 输入: 测试数据有多组,每组输入字符串s和 ...

  8. 九度OJ 1013:开门人和关门人 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5052 解决:2563 题目描述:     每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签到.签离记录,请 ...

  9. 如果这种方式导致程序明显变慢或者引起其他问题,我们要重新思考来通过 goroutines 和 channels 来解决问题

    https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/09.3.md 9.3 锁和 sync 包 在一些复杂的程序中,通常通 ...

  10. 我的Android进阶之旅------>Android实现音乐示波器、均衡器、重低音和音场功能

    本实例来自于<疯狂Android讲义>,要实现具体的功能,需要了解以下API: MediaPlayer  媒体播放器 Visualizer 频谱 Equalizer 均衡器 BassBoo ...