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. SPRING中的线程池ThreadPoolTaskExecutor(转)

    转自:https://blog.csdn.net/zhanglongfei_test/article/details/51888433 一.初始化 1,直接调用 ThreadPoolTaskExecu ...

  2. C# 特性(Attribute)之Flag特性

    本文参考自C# 位域[flags],纯属读书笔记,加深记忆 [Flags]的微软解释是“指示可以将枚举作为位域(即一组标志)处理.”其实就是在编写枚举类型时,上面附上Flags特性后,用该枚举变量是既 ...

  3. md5码加密(Python)

    import hashlib import hmac m = input('输入要加密内容:') md = hashlib.md5()#生成md5 hash对象 md.update(m.encode( ...

  4. Request笔记

    1 Request 的简介和运行环境 1.HttpServletRequest 概述 我们在创建 Servlet 时会覆盖 service()方法,或 doGet()/doPost(),这些方法都有两 ...

  5. Clojure编写一个阶乘程序 使用递归

    这是递归 (def f     (fn fb     [x]    (if (< x 2)      1      (* x (fb (- x 1)) )     )        )  ) ( ...

  6. javascript通过class获取元素

    1.getElementsByClassName 非IE6,7,8可以直接用自带的属性 getElementsByClassName,如果需要兼容 function getElementsByClas ...

  7. 关于Stream的Read方法

    一次做到一个关于使用DataContractJsonSerializer类的表述.其中需要用到MemoryStream数组读取.发生数组溢出错误,这里特记录一笔: public static clas ...

  8. Oracle 架构

  9. Python-常用模块及简单的案列

    1.模块   函数的优点之一,就是可以使用函数将代码块与主程序分离,通过给函数指定一个描述性的名称,并将函数存储在被称为模块的独立文件中,再将模块导入主程序中,通过import语句允许在当前运行的程序 ...

  10. Freemarker 最简单的例子程序

    首先导入包,freemarker.jar   下载地址:  freemarker-2.3.18.tar.gz http://cdnetworks-kr-1.dl.sourceforge.net/pro ...