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. 阿里云API网关(7)开发指南-API参考

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

  2. kafka---broker 保存消息

    1 .存储方式 物理上把 topic 分成一个或多个 patition(对应 server.properties 中的 num.partitions=3 配置),每个 patition 物理上对应一个 ...

  3. Window7系统下安装jdk

    根据电脑的操作系统下载相对于的jdk版本(32位或64位),我安装的是:java_jdk1.7 [计算机]——[属性]——[高级系统设置]——高级——[环境变量] 系统变量——>新建JAVA_H ...

  4. Django实现 省 市 县 自关联的下拉级联

    前端部分: 三个下拉拉菜单进行级联 <body> <select id="province" > <option value="" ...

  5. django的models模型 关联关系和关系查询

    模型类关系 关系字段类型 关系型数据库的关系包括三种类型: ForeignKey:一对多,将字段定义在多的一端中. ManyToManyField:多对多,将字段定义在两端中. OneToOneFie ...

  6. hdu 6205 card card card

    https://vjudge.net/contest/184514#problem/L题意:排成一行的一堆牌,每堆牌都有一定数量的牌,然后每堆牌对应有一个惩罚值.一开始所有的牌都是正面向下的,并且在游 ...

  7. POJ-2996 Help Me with the Game---模拟棋子

    题目链接: https://vjudge.net/problem/POJ-2996 题目大意: 给出白方和黑方的棋子和对应的坐标,输出该副棋盘的样子 1,棋盘中大写字母表示的是白方棋子,小写是黑方.2 ...

  8. 在windows系统之中查看目前已安装的更新

    方法1:使用PowerShell get-hotfix 方法2:使用cmd systeminfo.exe 参考链接

  9. OpenGL 背面剔除

    在OpenGL种可使用glEnable(GL_CULL_FACE)开启背面剔除功能,即把那些我们看不见的面删除.但在剔除之前我们需要定义正面和背面,这个可以用法线来理解.在数学学科中,法线是用右手法则 ...

  10. C#扩展方法(转)

    扩展方法使您能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型." 这是msdn上说的,也就是你可以对String,Int,DataRo ...