Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,

If nums = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

有重复值的序列的所有子集.

核心想法是:

把 [2,2] 看成1个特殊的元素,它不想其他元素只有两种状态(存在,不存在).

This element([2, 2]) has more than two choices: you can either NOT put it into the subset, or put ONE 2 into the subset, or put TWO 2s into the subset.

上面是老外的解释,很精彩且到位.

这题,对我来说,即使知道想法,编这个东西难度也挺大.

仔细考察人家的代码后,发现下面的事实:

下面程序生成序列的顺序如下:

[[]]->[[],[1]]->[[],[1],[2],[2,2],[1,2],[1,2,2]]
|<- []->| |<- [1] ->| |<- []->| 这个地方代表 [2],[2,2] 是由 [] 生成的;
|<-[1]->| 这个地方代表 [1,2],[1,2,2] 是由 [1] 生成的.

了解了上述生成顺序,下面的代码就好理解了.

人家想法,复写人家代码(那想法好难实现).

有三个亮点:

  1. i += count; // smart~!
  2. 以 i 为 base,对 count 计数: while (count + i < A.size() && A[count + i] == A[i]) count++;
  3. preN 保存了 res 未改变前的长度;
  4. 用一个 vector 保存 res[k]: vector<int> inst = res[k]; // [].
    • 这个太重要了,因为可以实现 [1] -> [1,2](store this in res) -> [1,2,2](store this value in res again base on [1, 2])

\(O(n^2)\) time, \(O(1)\) extra space.

// sample: A = [1, 2, 2]
// 即使知道想法,编这个东西难度也挺大
// 把 2,2 看成一个特殊的元素.
// 可以出现1个2, 也可以出现两个2
// 下面程序生成序列的顺序如下:
// [[]]->[[],[1]]->[[],[1],[2],[2,2],[1,2],[1,2,2]]
// |<- []->| |<- [1] ->|
vector<vector<int>> subsetsWithDup(vector<int>& A) {
sort(A.begin(), A.end());
vector<vector<int> > res = { {}}; //[[],] for (int i = 0; i < A.size();) { // 以 indx = i 为base
// 在A中向后查和A[i]相同的元素个数 -> count
int count = 0;
while (count + i < A.size() && A[count + i] == A[i]) count++; // res 未改之前的长度 -- 初始时,这个熊样--> res = [[]]
int preN = res.size();
for (int k = 0; k < preN; k++) {
vector<int> inst = res[k]; // [] for (int j = 0; j < count; j++) {
// e.g. 若 inst = []
// when j = 0, inst = [2]
// j = 1, inst = [2, 2]
// inst 所代表的 array 都将送入 res
inst.push_back(A[i]);
res.push_back(inst);
}
}
i += count; // smart~!
}
return res;
}

90. Subsets II(中等,编写代码有难度)的更多相关文章

  1. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  2. 90. Subsets II

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  3. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...

  4. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  5. 【LeetCode】90.Subsets II

    Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...

  6. LeetCode Problem 90. Subsets II

    python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...

  7. Leetcode#90 Subsets II

    原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...

  8. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  9. 78. Subsets 90. Subsets II

    1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...

随机推荐

  1. 详解get请求和post请求参数中文乱码的解决办法

    首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...

  2. mybatis的generator中xml配置问题

    <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.sung.risk.model.biz" t ...

  3. linux系统下的SVN安装

    1.直接安装 # sudo apt-get install subversion 2. 创建版本库 # sudo mkdir /home/svn # sudo svnadmin create /hom ...

  4. NetSNMP开源代码学习——mib扩展

    扩展MIB库关于MIB库的扩展网络文章非常多,这里我主要参考了http://blog.csdn.net/qq_27204267/article/details/51595708,这篇文章介绍的比较简单 ...

  5. 28.C++- 单例类模板(详解)

    单例类 描述 指在整个系统生命期中,一个类最多只能有一个实例(instance)存在,使得该实例的唯一性(实例是指一个对象指针)  , 比如:统计在线人数 在单例类里,又分为了懒汉式和饿汉式,它们的区 ...

  6. chm 转 txt

    CHM格式转TXT,如果在Windows下可使用命令行实现,为叙述方便,以笔者机器为例,在 E:\11 文件夹下有 123.chm 这个文件,按如下操作将这个 CHM 转成 TXT 文件. 第一步: ...

  7. 原来你是这样的Promise

    1. Promise简介 promise是异步编程的一种解决方案,它出现的初衷是为了解决回调地狱的问题. 打个比方,我需要: --(延迟1s)--> 输出1 --(延迟2s)--> 输出2 ...

  8. Web SCADA 电力接线图工控组态编辑器

    前言 SVG并非仅仅是一种图像格式, 由于它是一种基于XML的语言,也就意味着它继承了XML的跨平台性和可扩展性,从而在图形可重用性上迈出了一大步.如SVG可以内嵌于其他的XML文档中,而SVG文档中 ...

  9. [LeetCode] Max Chunks To Make Sorted 可排序的最大块数

    Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into som ...

  10. [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...