Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • 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],
[]
]

Subscribe to see which companies asked this question

这题有反复元素,但本质上,跟上题非常相似。也能够用相似的方法处理:

class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> ans(1, vector<int>());
sort(nums.begin(), nums.end()); int pre_size = 0;
for (int i = 0; i < nums.size(); i++)
{
int n = ans.size();
for (int j = 0; j < n; j++) //第二个for循环是为了求得包括nums[i]的全部子集
{
if (i == 0 || nums[i] != nums[i -1] || j>=pre_size)
{
ans.push_back(ans[j]); //在尾部又一次插入ans已经存在的子集
ans.back().push_back(nums[i]); //将nums[i]加入进去
}
}
pre_size = n;
}
return ans;
}
};

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

LeetCode 90:Subsets II的更多相关文章

  1. LeetCode OJ:Subsets II(子集II)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  2. LeetCode OJ:Subsets(子集)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  3. LeetCode(90) Subsets II

    题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  4. lintcode 中等题:subsets II 带重复元素的子集

    题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...

  5. [LeetCode 题解]: Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. LeetCode OJ:Permutations II(排列II)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. 【LeetCode 90】子集 II

    题目链接 [题解] 我们在枚举下一个要取哪个数字的时候. 如 1112233 for (int i = start;i<=n;i++) //其中start-1是上一次取的位置. 如果i>s ...

  8. 90. Subsets II

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  9. Leetcode之回溯法专题-90. 子集 II(Subsets II)

    Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...

随机推荐

  1. 2018-2019 2 20165203 《网络对抗技术》 Exp1 PC平台逆向破解

    2018-2019 2 20165203 <网络对抗技术> Exp1 PC平台逆向破解 实验要求 1.掌握NOP, JNE, JE, JMP, CMP汇编指令的机器码 2.掌握反汇编与十六 ...

  2. canvas画多边形

    <canvas id = "myCanvas" width = '500' height = '500'> Canvas画正多边形 </canvas>< ...

  3. python全栈开发知识点补充for else和while else如果不是除正常以外的其他方式退出循环,那么else语句就会被执行。

    如果不是除正常以外的其他方式退出循环,那么else语句就会被执行. 也就是循环体内没有break语句.return语句.和其他异常语句的执行. for   else >>> for ...

  4. zstu 4247-萌新的旅行

    题目大意: zstu的萌新们准备去自助旅行,他们租了一辆吉普车,然后选择了n个城市作为游览地点.然后他们惊喜的发现他们选择的城市刚好绕城一个环. 也就是说如果给所有城市按照0,1,2,……,n-1编号 ...

  5. MQ的订阅模式

    一:介绍 1.模式 2.使用场景 一个生产者,多个消费者 每一个消费者都有自己的队列 生产者没有直接把消息发送给队列,而是发送到了交换机 每一个队列都要绑定到交换机 可以实现一个消息被多个消费者消费. ...

  6. HDU4372-Count the Buildings【第一类Stirling数】+【组合数】

    <题目链接> <转载于 >>> > 题目大意: N座高楼,高度均不同且为1~N中的数,从前向后看能看到F个,从后向前看能看到B个,问有多少种可能的排列数. 0 ...

  7. JSONObject基本内容(一)

    参考资料:http://swiftlet.net/archives/category/json,十分感谢~ 平时做项目,难免有遇到使用json对象的时候,这个东西不难,但是一不使用久了,就会忘记,所以 ...

  8. 专业方向系列-00-Python与有限元初探

    案例1 给出4个弹簧的劲度系数,离散后,求其总的刚度矩阵. 代码: import numpy as np k1, k2, k3, k4 = 500, 250, 2000, 1000 ki = np.a ...

  9. j2me必备之网络开发数据处理

    第9章 无线网络开发MIDP提供了一组通用的网络开发接口,用来针对不同的无线网络应用可以采取不同的开发接口.基于CLDC的网络支持是由统一网络连接框架(Generic Connection Frame ...

  10. SimpleDateFormat 格式化参数说明

    字母 日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month July; Jul; 07 w 年中的周数 Number 27 W ...