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. [转]DBA,SYSDBA,SYSOPER三者的区别

    原文地址:http://www.oracleonlinux.cn/2010/02/dba_sysdba_sysoper/ 什么是DBA?什么是SYSDBA,什么又是SYSOPER?三者究竟有何联系呢? ...

  2. easyui_动态添加隐藏toolbar按钮

    目标:动态添加隐藏toolbar,比如根据权限动态显示新增.修改.删除按钮等 思路:先初始化toolbar的所有按钮,加载datagrid其它信息,再根据权限显示隐藏toolbar按钮 步骤: 1.加 ...

  3. array,vertor,arraylist,hashable,hashmap等几个易混淆概念的区别

    Array可以存放Object和基本数据类型,但创建时必须指定数组的大小,并不能再改变, Vertor是放的Object. Vertor一维,Hashmap/Hashtabe二维: Vertor/Ar ...

  4. url的编码问题

    JQuery中 编码 var url = 'folder/index.html?param=#23dd&noob=yes'; var encodedUrl = encodeURICompone ...

  5. SpringMVC配置

    博客园 闪存 首页 新随笔 联系 管理 订阅 随笔- 4  文章- 1  评论- 0  搭建springmvc框架的另一种思路 在一个完整的项目里搭建springmvc框架的时候, 通常情况下,初学者 ...

  6. TCP/IP详解 学习七

    静态选路的前提: 1)         网络比较小 2)         网络之间单点连接 3)         网络之间没有多余的路由 动态选路协议,用于路由器之间的通信,有以下几种: 1)     ...

  7. php中图片文件的导入,上传与下载

    ---------------------------------------------图片的导入-------------------------------------------------- ...

  8. html5新标签转

    HTML 5 是一个新的网络标准,目标在于取代现有的 HTML 4.01, XHTML 1.0 and DOM Level 2 HTML 标准.它希望能够减少浏览器对于需要插件的丰富性网络应用服务(p ...

  9. SSH协议及其应用

    SSH协议及其应用 原文作者:阮一峰 链接: http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruany ...

  10. FCKeditor漏洞利用

    FCKeditor漏洞利用 查看编辑器版本 FCKeditor/_whatsnew.html fckeditor/editor/dialog/fck_about.html —————————————— ...