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:

  1. [
  2. [2],
  3. [1],
  4. [1,2,2],
  5. [2,2],
  6. [1,2],
  7. []
  8. ]

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

核心想法是:

把 [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. 下面程序生成序列的顺序如下:
  2. [[]]->[[],[1]]->[[],[1],[2],[2,2],[1,2],[1,2,2]]
  3. |<- []->| |<- [1] ->|
  4. |<- []->| 这个地方代表 [2],[2,2] 是由 [] 生成的;
  5. |<-[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.

  1. // sample: A = [1, 2, 2]
  2. // 即使知道想法,编这个东西难度也挺大
  3. // 把 2,2 看成一个特殊的元素.
  4. // 可以出现1个2, 也可以出现两个2
  5. // 下面程序生成序列的顺序如下:
  6. // [[]]->[[],[1]]->[[],[1],[2],[2,2],[1,2],[1,2,2]]
  7. // |<- []->| |<- [1] ->|
  8. vector<vector<int>> subsetsWithDup(vector<int>& A) {
  9. sort(A.begin(), A.end());
  10. vector<vector<int> > res = { {}}; //[[],]
  11. for (int i = 0; i < A.size();) {
  12. // 以 indx = i 为base
  13. // 在A中向后查和A[i]相同的元素个数 -> count
  14. int count = 0;
  15. while (count + i < A.size() && A[count + i] == A[i]) count++;
  16. // res 未改之前的长度 -- 初始时,这个熊样--> res = [[]]
  17. int preN = res.size();
  18. for (int k = 0; k < preN; k++) {
  19. vector<int> inst = res[k]; // []
  20. for (int j = 0; j < count; j++) {
  21. // e.g. 若 inst = []
  22. // when j = 0, inst = [2]
  23. // j = 1, inst = [2, 2]
  24. // inst 所代表的 array 都将送入 res
  25. inst.push_back(A[i]);
  26. res.push_back(inst);
  27. }
  28. }
  29. i += count; // smart~!
  30. }
  31. return res;
  32. }

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. VirtualBox的共享文件夹功能的使用演示

    演示环境 虚拟机 Oracle VM VirtualBox 宿主机 Windows 客户机 Linux 以下图片演示中使用的Linux客户机为CentOS.对于Debian系统的客户机,主要在安装增强 ...

  2. Extensions in UWP Community Toolkit - WebViewExtensions

    概述 UWP Community Toolkit Extensions 中有一个为 WebView 提供的扩展 - WebViewExtensions,本篇我们结合代码详细讲解 WebView Ext ...

  3. 搭建ssm项目框架

    [声明]转载注明链接,源码联系公众号:aandb7获取 [此处组织名groupId:com.dayuanit,可替换公司域名:项目名artifactid:...] 此处第二个配置文件选择maven安装 ...

  4. JVMGC机制

    GC 是JVM的垃圾回收器.与C/C++不同,java程序员无需考虑太多内存分配的位置,更不用考虑内存释放的机制,java对象内存的申请和释放都有JVM托管.JVM的内存释放机制就是GC. GC的过程 ...

  5. [LeetCode] Next Greater Element III 下一个较大的元素之三

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  6. js 的作用域 域解析 分析

    作用域链   函数每次执行时,浏览器都会在函数中开启一个地方用来存储函数内的局部数据.(声明在函数内的局部变量),这个地方就叫做作用域([scopes])   作用域链 变量与函数的查找规则:当我们在 ...

  7. 虚拟机工作站创建虚拟机并安装Linux教程

    前言: 今天开始学习一下Linux,之前早就想看,但是一直没时间,最近把其他知识整理完了,终于有时间来看一下Linux了. 本节只是安装虚拟机工作站,虚拟机,和Linux操作系统的过程,详细的记录了我 ...

  8. [NOI2016]循环之美

    Description 牛牛是一个热爱算法设计的高中生.在他设计的算法中,常常会使用带小数的数进行计算.牛牛认为,如果在 k  进制下,一个数的小数部分是纯循环的,那么它就是美的.现在,牛牛想知道:对 ...

  9. ●BZOJ 2049 [Sdoi2008]Cave洞穴勘测

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题解: LCT入门题 就是判两个点是否在同一颗树里 代码: #include<c ...

  10. 【NOIP2016】换教室

    题目描述 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 i ( 1≤ i≤n)个时同段上, 两节内容相同的课 ...