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. iOS边练边学--多线程NSOperation介绍,子类实现多线程的介绍(任务和队列),队列的取消、暂停(挂起)和恢复,操作依赖与线程间的通信

    一.NSOperation NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加 ...

  2. C# 全角符号转半角

    public static string SBCCaseToNumberic(string SBCCase) { char[] c = SBCCase.ToCharArray(); ; i < ...

  3. 【CodeForces 613A】Peter and Snow Blower

    题 题意 给出原点(不是(0,0)那个原点)的坐标和一个多边形的顶点坐标,求多边形绕原点转一圈扫过的面积(每个顶点到原点距离保持不变). 分析 多边形到原点的最小距离和最大距离构成的两个圆之间的圆环就 ...

  4. oracle基本语句

    ALTER TABLE SCOTT.TEST RENAME TO TEST1--修改表名 ALTER TABLE SCOTT.TEST RENAME COLUMN NAME TO NAME1 --修改 ...

  5. 【poj3608】 Bridge Across Islands

    http://poj.org/problem?id=3608 (题目链接) 题意 求两凸包间最短距离 Solution 难写难调,旋转卡壳,还真是卡死我了. 先分别选出两凸包最上点和最下点,从这两点开 ...

  6. poj 1743 二分答案+后缀数组 求不重叠的最长重复子串

    题意:给出一串序列,求最长的theme长度 (theme:完全重叠的子序列,如1 2 3和1 2 3  or  子序列中每个元素对应的差相等,如1 2 3和7 8 9) 要是没有差相等这个条件那就好办 ...

  7. [NOIP2010] 提高组 洛谷P1541 乌龟棋

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...

  8. 多线程下载的原理&断点下载的原理

    1)多线程下载说明:

  9. 输入你的性别,身高及体重,判断你的身材是否标准。(用if......else语句)

    Console.WriteLine("请输入你的性别,身高和体重:"); string s = Console.ReadLine(); double h = double.Pars ...

  10. Extreme Learning Machine(ELM)的工程哲学

    Extreme Learning Machine(ELM)的工程哲学 David_Wang2015 发布于2015年5月6日 11:29 工程问题往往需要的是一定精度范围内的结果,而不是“真正的”结果 ...