90. Subsets II(中等,编写代码有难度)
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] 生成的.
了解了上述生成顺序,下面的代码就好理解了.
人家想法,复写人家代码(那想法好难实现).
有三个亮点:
i += count; // smart~!- 以 i 为 base,对 count 计数:
while (count + i < A.size() && A[count + i] == A[i]) count++; - preN 保存了 res 未改变前的长度;
- 用一个 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(中等,编写代码有难度)的更多相关文章
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 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 ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 78. Subsets 90. Subsets II
1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
随机推荐
- EasyUI 数据网格行过滤
前段时间发现一个GridView很有用的功能,可以筛选数据,实现起来很简单 一.添加一个引用,这个可以自己去网上下载 <script type="text/javascript&quo ...
- django关闭调试信息,打开内置错误视图
1 内置错误视图 Django内置处理HTTP错误的视图,主要错误及视图包括: 404错误:page not found视图 500错误:server error视图 400错误:bad reques ...
- centos单机安装zookeeper+kafaka
环境如下: CentOS-7-x86_64zookeeper-3.4.11kafka_2.12-1.1.0 一.zookeeper下载与安装1)下载zookeeper [root@localhost ...
- hdu-3348 coins---贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3348 题目大意: 给你一个价格,还有面值分别为1,5,10,50,100(单位:毛)纸币的数量,要你 ...
- python手动设置递归调用深度
python超出递归深度时会出现异常: RuntimeError: maximum recursion depth exceeded python默认的递归深度是很有限的,大概是900当递归深度超过这 ...
- Centos MySQL数据库迁移详细步骤
其实迁移数据库,一般用sql文件就行,把A服务器数据库的表结构和数据等等导出,然后导入到B服务器数据库, 但是这次数据文件过大,大约有40个G,使用命令行导入,效果不是很好,经常在执行过程中报错.卡死 ...
- [Micropython]TPYBoard v10x拼插编程实验 点亮心形点阵
一.什么是TPYBoard开发板 TPYBoard是以遵照MIT许可的MicroPython为基础,由TurnipSmart公司制作的一款MicroPython开发板,它基于STM32F405单片机, ...
- 《C++ Primer》学习笔记:迭代器介绍
<C++Primer>(第五版)中,3.4.1的例题中使用一个名为text的字符串向量存放文本文件中的数据,输出text中的内容,刚开始我这样写: #include <iostrea ...
- [Codeforces 864B]Polycarp and Letters
Description Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s con ...
- [AHOI 2012]树屋阶梯
Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营.小龙分配的树屋建立在一颗高度为 ...