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. Oracle update 执行更新操作后的数据恢复

    操作数据库,经常会出现误操作,昨天执行的更新操作之后发现更新错了,只能想办法数据恢复了,现在整理一下 第一步:查询执行更新操作的时间 select r.FIRST_LOAD_TIME,r.* from ...

  2. MFC基础

    入门博客:http://www.cnblogs.com/qinfengxiaoyue/category/451679.html 消息机制:http://www.cnblogs.com/qinfengx ...

  3. 2018 6年iOS开发常用的三方库

    开发一般APP必备三方库,省力秘籍!!!本篇文章会经常更新最新常用的三方. 1.网络请求库 AFNetworking https://github.com/AFNetworking/AFNetwork ...

  4. java字符串类型常量拼接与变量拼接的区别

    前言 首先看下下面代码结果是什么? package cn.demo_01; public class StringDemo02 { public static void main(String[] a ...

  5. url,href,src之间的区别

    发现自己居然没把url.href.src关系及使用搞清楚,今天就理一下.主要包括:url.src.href定义以及使用区别. URL(Uniform Resource Locator) 统一资源定位符 ...

  6. Git Bash

    Git Bash是Git的命令行工具,可以执行Git的所有命令,但是当我们想把一个URL粘贴到Git Blash时,Ctrl+V或者右键粘贴不起作用了 方法1-使用快捷键"Insert&qu ...

  7. 基于Mysql 5.7 GTID 搭建双主Keepalived 高可用

    实验环境 CentOS 6.9 MySQL 5.7.18 Keepalived v1.2.13 拓扑图 10.180.2.161 M1 10.180.2.162 M2 10.180.2.200 VIP ...

  8. css 宽高自适应的div 元素 如何居中 垂直居中

    在我们 编写css 样式的时候经常会遇见一个问题 那就是一个 宽高未知的元素 要让他 垂直居中如何实现这个呢 下面是我常用的两种方法 上代码 下面的是 结构代码 <div class=" ...

  9. Linux服务器断电导致挂载及xfs文件损坏的修复方法

    系统文件损坏后进入紧急修复模式,无法进行维护工作 welcome to emergency mode!after logging in ,type "journalctl -xb" ...

  10. shell编程-邮件发送设置

    在linux 运维过程中,经常会写一些脚本监控一些服务器的状态,如监控redis 主从切换,redis 宕机等,当事件发生时,应该发送邮件通知到相对应的管理员,因此就需要搭建邮件服务,使linux 能 ...