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. GIT入门笔记(11)- 多种撤销修改场景和对策--实战练习

    1.检查发现目前没有变化$ git statusOn branch masternothing to commit, working tree clean $ cat lsq.txt2222 2.修改 ...

  2. 阿里云API网关(8)开发指南-SDK下载

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  3. 使用java 打印日历

    package hangshu; /* * 打印从1900年到2.year年的日历 */ import java.util.Scanner; public class Calender { publi ...

  4. mongodb 索引的基本命令

    mongodb的索引: 在数据量超大的时候,能够极大的增快查询速率,但是会降低更新效率.建立索引: db.集合.ensureIndex({属性:1}) //1代表升序 -1代表降序 db.集合.ens ...

  5. Python基础数据类型之int、bool、str

    数据类型:int  bool  str  list  元祖  dict  集合 int:整数型,用于各种数学运算. bool:只有两种,True和False,用户判断. str:存储少量数据,进行操作 ...

  6. Java-NIO(六):Channel聚集(gather)写入与分散(scatter)读取

    Channel聚集(gather)写入: 聚集写入( Gathering Writes)是指将多个 Buffer 中的数据“聚集”到 Channel. 特别注意:按照缓冲区的顺序,写入 positio ...

  7. JDK1.8源码(七)——java.util.HashMap 类

    本篇博客我们来介绍在 JDK1.8 中 HashMap 的源码实现,这也是最常用的一个集合.但是在介绍 HashMap 之前,我们先介绍什么是 Hash表. 1.哈希表 Hash表也称为散列表,也有直 ...

  8. [NOIp 2014]解方程

    Description 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) Input 输入文件名为equation .i ...

  9. Prison 监狱

    [题目描述]Caima 王国中有一个奇怪的监狱,这个监狱一共有 P 个牢房,这些牢房一字排开,第 i 个仅挨着第 i+1 个(最后一个除外).现在正好牢房是满的.上级下发了一个释放名单,要求每天释放名 ...

  10. [SDOI2010]代码拍卖会

    题目描述 随着iPig在P++语言上的造诣日益提升,他形成了自己一套完整的代码库.猪王国想参加POI的童鞋们都争先恐后问iPig索要代码库.iPig不想把代码库给所有想要的小猪,只想给其中的一部分既关 ...