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],
[]
]

解题:

先对S进行排序,然后继续按照Subsets I的思路,

由于出现了重复元素,需要分别考虑:

当出现的数字和上一个数字不同,保持原返回队列不变,对新出现的数字,分别插入已有数组的后面,形成新数组,加入返回值队列中;此时记录形成的新数组个数X;

当出现的数字和上一个数字相同,保持原返回队列不变,从原返回队列的后方遍历X个已有数组,对这X个数组插入这个相同的数字,形成新数组,加入返回队列中;X值不变;

代码:

 class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > ret;
ret.push_back(vector<int> ());
int duplicate_record = ; for (int i = ; i < S.size(); ++i) {
int pre_size = ret.size(); if (i== || S[i] != S[i-]) {
for (int j = ; j < pre_size; ++j) {
vector<int> new_item(ret[j]);
new_item.push_back(S[i]);
ret.push_back(new_item);
}
duplicate_record = ret.size() / ; } else if (S[i] == S[i-]) {
for (int j = pre_size - ; j >= pre_size - duplicate_record; --j) {
vector<int> new_item(ret[j]);
new_item.push_back(S[i]);
ret.push_back(new_item);
}
} } return ret;
}
};

【Leetcode】【Medium】Subsets II的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  6. 【leetcode刷题笔记】Subsets II

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

  7. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. 【leetcode刷题笔记】Subsets

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

  9. 【leetcode刷题笔记】Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. 【leetcode刷题笔记】Populating Next Right Pointers in Each Node II

    What if the given tree could be any binary tree? Would your previous solution still work? Note: You ...

随机推荐

  1. Java_方法的定义以及分类

    什么叫方法? 方法也叫做函数,实现某个功能 方法分类: 系统提供的方法: 常用的系统提供的方法:如:nextInt() next() nextDouble() print println()..... ...

  2. mysql 01

    CREATE TABLE student(sid INT,sname VARCHAR(10),ssex CHAR(2) )/*插入数据*/INSERT INTO student VALUES(1,'袁 ...

  3. IIS 5.0 和 6.0 的 ASP.NET 应用程序生命周期概述

    本主题概述 ASP.NET 应用程序的生命周期,列出了重要的生命周期事件,并描述了您编写的代码将如何适应于应用程序生命周期.本主题中的信息适用于 IIS 5.0 和 IIS 6.0.有关 IIS 7. ...

  4. 多线程FTP下载日志脚本

    #!/bin/bash ip_list=`cat $1` thead_num=5tmp_fifofile="/tmp/$$.fifo"mkfifo "$tmp_fifof ...

  5. Centos 从零开始 (三)

    8:连接阿里云. 需要用到 ssh指令进行远程登陆 [root@localhost ~]# service sshd start #如果没开启服务的话,需要开启服务. [root@localhost  ...

  6. C#语言-05.委托和事件

    a. 委托:是一种定义方法签名的类型,可以与具有兼容签名的任何方法关联.所谓兼容的方法,是指这个方法和委托的方法签名具有相同的返回类型和参数 i. 语法:delegate 方法签名; . 方法签名是方 ...

  7. Oracle中Merge into的用法实例讲解

    最近在做一个需求,就是涉及到表的问题,前端传过来一条数据,根据主键,查询数据库,如果不存在,那么久插入到数据库中一条,如果存在的话,就是以主键的方式,对数据库中的数据,进行更新. 拿到这个需求的时候, ...

  8. Springmvc file多附件上传 显示 删除操作

    之前项目需求要做一个多附件上传 并显示上传文件 带删除操作 一筹莫展之际搜到某个兄弟发的博客感觉非常好用被我copy下来了此贴算是改良版 再次感谢(忘记叫什么了时间也有点久没有历史记录了)先上图 基于 ...

  9. 来看看javaweb的自定义标签

    1.为什么需要自定义标签? jsp的简单标签其实就是jsp的自定义标签,主要作用就是移除jsp页面中的java代码,使得jsp页面只有标签和EL表达式,而没有java代码.利用自定义标签,可以使软件开 ...

  10. [Java反射基础一]Class类的使用

    任何一个类都是Class类的实例对象,这个实例对象有三种表示方式 第一种表示方式(任何一个类都有一个隐含的静态成员变量class): Class c1 = Foo.class; 第二种表示方式(已知该 ...