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的更多相关文章

  1. [Leetcode Week8]Subsets II

    Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...

  2. 【leetcode】Subsets II

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

  3. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  5. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  6. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  7. LeetCode OJ——Subsets

    http://oj.leetcode.com/problems/subsets/ 计算一个集合的子集,使用vector<vector<int> >,使用了进制的思想. #inc ...

  8. 深度优先搜索算法(DFS)以及leetCode的subsets II

    深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...

  9. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  10. leetcode 【 Subsets II 】python 实现

    题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. No ...

随机推荐

  1. 2-python基础

    1.第一个程序 新建一个python文件,然后写上代码,运行即可 print("hello world") 2.变量 变量就是存东西,供后面来用的 变量名只能是 字母.数字或下划线 ...

  2. AHB 总线问答(转)

    AHB总线问答 http://blog.163.com/huanhuan_hdu/blog/static/1352981182011625916845/ 仲裁:主设备可以在一个突发传输中解除HLOCK ...

  3. Tracer Deployment UVALive - 8271 二分图匹配

    复习二分图又想起了这道题,裸的二分图匹配,直接匈牙利算法就可以了,mark一下这个比较好用的稠密图匈牙利算法模板 题目:题目链接 AC代码: #include <iostream> #in ...

  4. poj 1862 2*根号(n1*n2)问题 贪心算法

    题意: 有n个数,要把其中2个数进行2*根号(n1*n2)操作,求剩下最小的那个数是多少? 哭诉:看题目根本没看出来要让我做这个操作. 思路: 每次把最大的,次大的拿出来进行操作 用"优先队 ...

  5. 【原创】关于高版本poi autoSizeColumn方法异常的情况

    之前使用的3.9版本,autoSizeColumn方法一切正常,现在切换到了3.15版本这个方法就出先了问题,问题如下,无法自动追踪所有的列. Exception in thread "ma ...

  6. selenium2断言类Assert的使用

    测试中断言的重要性 一.断言的作用: 1.断言也就是检查点,重在判断我们通过页面得出来的值与期望值是否相等,如果相等,则代表断言成功,程序会继续往下执行,如果不相等,则代表断言失败,程序就会在断言失败 ...

  7. src与href的区别

    href: 是指向网络资源所在位置,建立和当前元素(锚点)或当前文档(链接)之间的链接,用于超链接. src:是指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置:在请求src资源时会将其 ...

  8. luogu3389 【模板】高斯消元法

    #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> ...

  9. Maven项目下Tomcat插件选择方法

    1. 进入Tomcat官网:http://tomcat.apache.org/ 选择Maven plugin 2. 选择版本 3. 查看版本对应的插件版本: 有两种方式添加:如下图所示:

  10. day04_07 while循环01

    while循环结构: #while 条件: print("any") print("any") 死循环案例 num = 1 while num<=10 : ...