https://oj.leetcode.com/problems/subsets-ii/

求一个集合的子集,但集合中有重复元素。

求子集的问题,对应着数的二进制,相当于对二进制的一个遍历。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int> > ans;
if(S.size()==)
return ans;
int num = pow(,S.size());
sort(S.begin(),S.end());
for(int i = ; i<num; i++)
{
vector<int> ansPiece;
ansPiece = oneSubsetPiece(i,S);
bool flag = ;
for(int j = ; j<ans.size(); j++)
{
if(ansPiece == ans[j]) flag = ;
}
if(flag == )
ans.push_back(ansPiece);
}
return ans;
}
//find the num's binary
vector<int> oneSubsetPiece(int num,vector<int> &S)
{
vector<int> ansPiece;
int YuShu;
int ChuShu;
int index = ;
while(num)
{
YuShu = num %;
num = num/;
if(YuShu)
ansPiece.push_back(S[index]);
index++;
}
return ansPiece;
}
}; int main()
{
class Solution sol;
vector<int> num;
num.push_back();
num.push_back();
num.push_back();
sol.subsetsWithDup(num);
}

最新文章

  1. Apache2.4.6服务器安装及配置
  2. html学习记录之表格、表单基础
  3. GitHub下载的 Git Shell中出现的字符支持警告,将字体修改为新宋体即可
  4. Stationary point
  5. eclipse 设置jvm 内存
  6. 我的日常工具——gdb篇
  7. javascript 学习随笔6
  8. hdu2222Keywords Search (特里)
  9. scala与java的==的比较
  10. 【个人笔记】《知了堂》前端mySql基础
  11. cache 订单队列 - TP5
  12. UnityInspector显示扩展
  13. 浅谈 drop、truncate和delete的区别
  14. [转]Centos7 fastdfs/nginx 安装与配置
  15. Vue中的事件与常见的问题处理
  16. HDU 1556 Color the ball (数状数组)
  17. 简单的php自定义错误日志
  18. 纸壳CMS 3.0升级.Net Core 2.1性能大提升
  19. 牛客挑战赛30D 小A的昆特牌(组合数学)
  20. Zoj 3870——Team Formation——————【技巧,规律】

热门文章

  1. pandas处理大文本数据
  2. java工作环境配置jdk,idea
  3. 15Shell脚本—流程控制
  4. 如何封装RESTful Web Service
  5. python3.7 内置函数整理
  6. BZOJ 4368: [IOI2015]boxes纪念品盒
  7. JAVA 基础--开发环境 vscode 搭建
  8. MongoDB学习--&gt;Gridfs分布式存储&amp;DBRef关联查询
  9. dataTable组件使用
  10. day04 装饰器 迭代器&amp;生成器 Json &amp; pickle 数据序列化 内置函数