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. JDK的目录

    要想深入了解Java必须对JDK的组成, 本文对JDK6里的目录做了基本的介绍,主要还是讲解 了下JDK里的各种可执行程序或工具的用途 Java(TM) 有两个平台 JRE 运行平台,包括Java虚拟 ...

  2. hdu1535 SPFA

    2边SPFA 然后求和 #include<stdio.h> #include<string.h> #include<queue> #define INF 10000 ...

  3. Hibernate-org.hibernate.QueryException: could not resolve property: code of:

    查询的时候有个属性跟表里的字段不符合,没有完全匹配上.

  4. 19.Android之文件存储方法学习

    Android开发中会用到文件存储,今天来学习下. 先改下布局界面: <?xml version="1.0" encoding="utf-8"?> ...

  5. hdu 1166 敌兵布阵--BIT

    BIT模版题,学完直接刷毫无压力,水的不要不要的 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  6. codeforces 715B:Complete The Graph

    Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...

  7. HD1847 Good Luck in CET-4 Everybody!(巴什博弈)

    巴什博弈: 一堆物品n个,最多取m个,最少取1个,最后取走的人获胜 分析:只要保证取玩最后剩m+1个,则必定胜利,所以构造m+1,只要n是 m+1的倍数,则先手必败,每次先手取玩,后手可取使得剩下的仍 ...

  8. Intent和Intent Filter Context

    http://www.android-doc.com/reference/android/content/Intent.html An intent is an abstract descriptio ...

  9. html标签(一)

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

  10. ECSHOP手机号码或邮箱用户名都可以登录方法

    ECSHOP手机号码或邮箱用户名都可以登录方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-06-30   有不少人都在找支持ECShop用户名.邮箱或手号 ...