LeetCode OJ--Subsets II
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);
}
LeetCode OJ--Subsets II的更多相关文章
- [Leetcode Week8]Subsets II
Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- LeetCode OJ——Subsets
http://oj.leetcode.com/problems/subsets/ 计算一个集合的子集,使用vector<vector<int> >,使用了进制的思想. #inc ...
- 深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- leetcode 【 Subsets II 】python 实现
题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. No ...
随机推荐
- hibernate简介以及简单配置
Hibernate简介: Hibernate是一个开源对象关联关系映射的框架,他对JDBC做了轻量级的封装,使我们可以通过面向对象的思想操作数据库. 为什么要用Hibernate: 1: 对JDBC访 ...
- LeetCode之Weekly Contest 101
前一段时间比较忙,而且做这个对于我来说挺耗时间的,已经间隔了几期的没做总结了,后面有机会补齐.而且本来做这个的目的就是为了防止长时间不做把编程拉下,不在追求独立作出所有题了.以后完赛后稍微尝试下,做不 ...
- 模拟发送http请求的工具推荐
做网站开发时,经常需要发送请求来测试自己的代码是否OK,这时候模拟发送http请求的工具就起到了很大的作用.特别是需要在请求带header时就更加的有必要使用工具.下面推荐的工具有的是基于系统开发的程 ...
- Eclipse使用Mybatis-Generator插件
Mybatis-Generator插件极大地方便了我们的开发效率,不用每张表每个字段人工去敲,所以本文介绍使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件 版权 ...
- 【jenkins】jenkins执行nohup java报错
nohup:failed to run command 'java':No such file or directory 这是因为jenkins只认绝对路径.在shell里面有涉及到文件的都应该写成绝 ...
- drf 频率组件 META字典详情
drf频率组件 什么是频率 控制用户对某个url的请求频率,比如一分钟之内,只能访问三次 自定义频率规则 1.取出访问者ip 2.判断当前ip在不在访问字典中: 不在,则添加进去,返回True; 3. ...
- 10个MCU常用的基础知识
转自:http://bbs.21ic.com/icview-2659278-1-1.html 1.MCU有串口外设的话,在加上电平转换芯片,如MAX232.SP3485就是RS232和RS485接口了 ...
- selenium2-元素管理方式及解析
1.管理文件格式:yaml 2.Yaml里面的内容格式: 3.格式说明: baidu_input后面接上":",直接回车,然后空两格 type与value这两个key是固定 ...
- javaweb通过接口来实现多个文件压缩和下载(包括单文件下载,多文件批量下载)
原博客地址:https://blog.csdn.net/weixin_37766296/article/details/80044000 将多个文件压缩并下载下来:(绿色为修改原博客的位置) 注意:需 ...
- IDEA-常用插件,使用FindBugs寻找bug,代码分析
bug无处不在,但是我们总希望少一点bug. 最近发现了一款好用的寻找bug的插件,特此记下. 一.安装 路径:File-->Settings-->Plugins-->Browse ...