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. 【转】selenium自动化测试用例需要关注的几点(一)

    自动化测试设计简介  注:参看文章地址 我们在本章提供的信息,对自动化测试领域的新人和经验丰富的老手都是有用的.本篇中描述最常见的自动化测试类型, 还描述了可以增强您的自动化测试套件可维护性和扩展性的 ...

  2. 以cmd命令行方式执行php文件时,传递参数

    1. php自带的两个参数$argc, $argv: 1.1. $argv : (后面的v是variables的意思),传递进来的参数会以数组的方式保持在这个变量里 1.2. $argc : (后面的 ...

  3. restful api理解

    REST -- REpresentational State Transfer 直接翻译:表现层状态转移. 首先要明确一点:REST 实际上只是一种设计风格,它并不是标准. 0. REST不是&quo ...

  4. [转载+原创]Emgu CV on C# (五) —— Emgu CV on 局部自适应阈值二值化

    局部自适应阈值二值化 相对全局阈值二值化,自然就有局部自适应阈值二值化,本文利用Emgu CV实现局部自适应阈值二值化算法,并通过调节block大小,实现图像的边缘检测. 一.理论概述(转载自< ...

  5. 自动生成数据库字典(sql2008) 转自 飘渺の云海

    每次做项目的时候都要做数据字典,这种重复的工作实在很是痛苦,于是广找资料,终于完成了自动生成数据库字典的工作,废话少说,上代码. 截取一部分图片: 存储过程: SET ANSI_NULLS ON GO ...

  6. Java生成验证码(二)

    前一篇博客已经介绍了如何用Java servlet产生验证码,本篇继续介绍如何使用一些开源组件生成验证码 ————————————————————————————————————————————   ...

  7. canal —— 阿里巴巴mysql数据库binlog的增量订阅&消费组件

    阿里巴巴mysql数据库binlog的增量订阅&消费组件canal ,转载自  https://github.com/alibaba/canal 最新更新 canal QQ讨论群已经建立,群号 ...

  8. 十 DatagramChannel

    DatagramChannel是一个能收发UDP包的通道.因为UDP是无连接的网络协议,所以不能像其它通道那样读取和写入.它发送和接收的是数据包. 打开 DatagramChannel 下面是 Dat ...

  9. 一 NIO的概念

    Java NIO由下列几个核心部分组成: Channels(通道) Buffers(缓冲区) Asynchronous IO(异步IO) Channel 和 Buffer 基本上所有的IO在NIO中都 ...

  10. 简单工厂模式的C++、Java实现

    1.简单工厂模式UML UML如下: 图1. 简单工厂模式UML 2.C++实现 类视图如下: 图2. C++实现简单工厂模式类视图 其中,SimpleFactory实现为: Product * Si ...