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. Java--equals和 == 的比较和equals()、HashCode()的重写

    一. equals和 == 的比较 1.== 运算符 ① == 如果比较的是基本数据类型,则比较的是值. ② == 如果比较的是引用数据类型,则比较的是地址值. 2.equals ①它属于java.l ...

  2. python 面向对象基础和高级复习

    面向对象基础 面向对象编程 面向过程编程:类似于工厂的流水线 优点:逻辑清晰 缺点:扩展性差 面向对象编程:核心是对象二字,对象属性和方法的集合体,面向对象编程就是一堆对象交互 优点:扩展性强 缺点: ...

  3. LeetCode(165) Compare Version Numbers

    题目 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version ...

  4. 查看 EGLIBC 版本

    $ ldd --versionldd (Debian EGLIBC 2.13-38+deb7u1) 2.13

  5. jmeter中基于oracle的JDBC Request的使用

    前提条件: 1.有数据库:2.数据库中有表,例如testuser(userid,username,usepwd): 设置如下: 参考自:http://www.linuxidc.com/Linux/20 ...

  6. 可持久化treap(FHQ treap)

    FHQ treap 的整理 treap = tree + heap,即同时满足二叉搜索树和堆的性质. 为了使树尽可能的保证两边的大小平衡,所以有一个key值,使他满足堆得性质,来维护树的平衡,key值 ...

  7. Python虚拟机函数机制之扩展位置参数和扩展键参数(六)

    扩展位置参数和扩展键参数 在Python虚拟机函数机制之参数类别(三)的例3和例4中,我们看到了使用扩展位置参数和扩展键参数时指示参数个数的变量的值.在那里,我们发现在函数内部没有使用局部变量时,co ...

  8. python单例模式的几种实现方法

    单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...

  9. Freemarker的循环通过assign指令引入计数变量

    这里是一个jeecms框架的前台的一个内容列表集,因为不是每个内容子项符合要求,而且需要统计符合要求的子项个数,仿照java的for循环,需要在循环前声明一个计数变量,这就需要使用Freemaker的 ...

  10. (手写)mybatis 核心配置文件和接口不在同一包下的解决方案

    smart-sh-mybatis项目 app.xml文件中此处配置为: <!-- 从整合包里找,org.mybatis:mybatis-spring:1.2.4 --> <!-- s ...