题目:

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;
}
};

最新文章

  1. Jquery mobiscroll 移动设备(手机)wap日期时间选择插件以及滑动、滚动插件
  2. POJ 2503 字典树
  3. 《Diagnostic use of facial image analysis software in endocrine and genetic disorders: review, current results and future perspectives》学习笔记
  4. 我的Unity学习路线
  5. Com和DCOM
  6. 常见错误总结_1_对java类进行修改后,无法按修改的类型加载
  7. Android surfaceview详解
  8. Gabor滤波器学习
  9. nagios监控mysql主从状态
  10. tableView滑动到底部
  11. 轻松几句搞定【Javascript中的this指向】问题
  12. 【Java一看就懂】浅克隆和深克隆
  13. [Luogu P1564] 膜拜
  14. 【2018.08.13 C与C++基础】网络通信:阻塞与非阻塞socket的基本概念及简单实现
  15. contextlib 上下文管理器
  16. java基础24 线程、多线程及线程的生命周期(Thread)
  17. vue中封装axios方法
  18. 9.使用GetData,Children实现对ZNode的监控
  19. 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记
  20. (转)python之os,sys模块详解

热门文章

  1. 手把手实现andriod应用增量升级
  2. MySQL 原理性
  3. python opener代理
  4. TP框架---thinkphp模型
  5. ICMP控制报文协议
  6. 【BZOJ3745】[Coci2015]Norma cdq分治
  7. springcloud微服务实战--笔记--1、基础知识
  8. EasyPlayer RTSP播放器对RTSP播放地址url的通用兼容修改意见
  9. 在WePY中实现了小程序的组件化开发,组件的所有业务与功能在组件本身实现,组件与组件之间彼此隔离,上述例子在WePY的组件化开发过程中,A组件只会影响到A所绑定的myclick
  10. (转)php 根据url自动生成缩略图并处理高并发问题