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