Subsets II

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

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If S = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
 
主要考虑去重,最简单的想法,在递归添加元素的时候,判断该元素是否已经出现过了
 
 
 
 class Solution {

 public:

     vector<vector<int> > subsetsWithDup(vector<int> &S) {

         vector<vector<int> > result;

         vector<int> tmp;

         sort(S.begin(),S.end());

         getSubset(result,S,,tmp);

         return result;

     }

     void getSubset(vector<vector<int> > &result,vector<int> &S,int index,vector<int> tmp)

     {

         if(index==S.size())

         {

             for(int i=;i<result.size();i++)

             {

                 if(result[i]==tmp)

                 return;

             }

             result.push_back(tmp);

             return;

         }

         getSubset(result,S,index+,tmp);

         tmp.push_back(S[index]);

         getSubset(result,S,index+,tmp);

     }

 };
 
考虑在寻找子集时,就去重,按照下面的方式进行。
假设1,2,3,3
初始时,什么都没选[]
当只有一个元素时:[1],[2],[3]重复的被去除
当有两个元素时:[12],[13],[23],[33]
当有三个元素时:[123],[133],[233]
 
可以按照如下的递归算法进行:
 
 
 
 class Solution {

 public:

     vector<vector<int> > subsetsWithDup(vector<int> &S) {

         vector<vector<int> > result;

         vector<int> tmp;

         sort(S.begin(),S.end());

         getSubset(result,S,,tmp);

         return result;

     }

     void getSubset(vector<vector<int> > &result,vector<int> &S,int index,vector<int> tmp)

     {

         result.push_back(tmp);

         for(int i=index;i<S.size();i++)

         {

             if(i>index&&S[i]==S[i-])continue;

             tmp.push_back(S[i]);

             getSubset(result,S,i+,tmp);

             tmp.pop_back();

         }
} };
 
 
 
 
 
 

【leetcode】Subsets II的更多相关文章

  1. 【leetcode】Subsets II (middle) ☆

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

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. 【LeetCode】课程表 II

    [问题]现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程.例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及 ...

  4. 【Leetcode】【Medium】Subsets II

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

  5. 【leetcode】Subsets (Medium) ☆

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  6. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  7. 【leetcode】N-Queens II

    N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...

  8. 【leetcode】Subsets

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  9. 【LeetCode】 Subsets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

随机推荐

  1. JS模式:Mixin混合模式,=_=!就是常见的Object.create()或者_extend()

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. BZOJ-2038 小Z的袜子(hose) 莫队算法

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MB Submit: 5573 Solved: 2568 [Subm ...

  3. [NOIP2008] 提高组 洛谷P1155 双栈排序

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  4. OpenJudge 7627 鸡蛋的硬度

    描述 最近XX公司举办了一个奇怪的比赛:鸡蛋硬度之王争霸赛.参赛者是来自世 界各地的母鸡,比赛的内容是看谁下的蛋最硬,更奇怪的是XX公司并不使用什么精密仪器来测量蛋的硬度,他们采用了一种最老土的办法- ...

  5. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

  6. mysql存储过程的学习

    平时在工作中写过很多存储过程,但有时候对某些存储过程还是有些困惑的,所以发一篇文章记录下. 标准存储过程写法 create procedure`myQueryTask`( IN Task_No VAR ...

  7. javaIO(二)

    在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流. 在java.io包中流的操作主要有字节流.字符流两大类,两类都 ...

  8. JAVA反射机制—学习总结

    最近收到很多关于Java反射机制的问题留言,其实Java反射机制技术方面没有太多难点,或许是大家在学习过程中遗漏了细小知识点,导致一些问题无法彻底理解,现在我们简单的总结一下,加深印象.什么是反射机制 ...

  9. Boost的状态机库教程(1)

    介绍 Boost状态机库一个应用程序框架,你可以用它将UML状态图快速的转换为可执行的c++代码,而不需要任何的代码生成器.它支持几乎所有的UML特征,可以直接了当的转换,并且转换后的c++代码就像对 ...

  10. memcached安装和php-memcached扩展安装.update.2014-08-15

    服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.3.0 .下载官网:http://www.danga.com另外,Memcache用到了libevent这个库用于 ...