LeetCode 90:Subsets II
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的更多相关文章
- LeetCode OJ:Subsets II(子集II)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- LeetCode OJ:Subsets(子集)
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- LeetCode(90) Subsets II
题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- lintcode 中等题:subsets II 带重复元素的子集
题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...
- [LeetCode 题解]: Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode OJ:Permutations II(排列II)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode 90】子集 II
题目链接 [题解] 我们在枚举下一个要取哪个数字的时候. 如 1112233 for (int i = start;i<=n;i++) //其中start-1是上一次取的位置. 如果i>s ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- Leetcode之回溯法专题-90. 子集 II(Subsets II)
Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
随机推荐
- python 全栈开发,Day105(路飞其他数据库表结构,立即结算需求)
考试第三部分:Django 16. 列列举你熟悉的Http协议头以及作用.(1分) Accept-Charset: 用于告诉浏览器,客户机采用的编码 Host: 客户机通过这个头告诉服务器,想访问的 ...
- MVC开发中的常见错误-07-“System.IO.DirectoryNotFoundException”类型的未经处理的异常在 mscorlib.dll 中发生
“System.IO.DirectoryNotFoundException”类型的未经处理的异常在 mscorlib.dll 中发生 其他信息: 未能找到路径“F:\Users\home\Docume ...
- 使用匿名内部类调用start方法
package chapter03;//类实现接口public class WD implements Runnable{//重写接口的方法 @Override public void run() { ...
- 去掉m3u8的片头和片尾
# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ m3u8 # pip3 install -i https://mirrors.ali ...
- 最近关于mysql的造型,binlog使用,以及阿里云上线数据处理错误导致被处罚的思考
因团队中成员,上线代码时,不小心将数据表中吃掉物理的数据清空,导致被单位处罚,痛定思痛,我们应该如何上线,还需要准备哪些技能? 1.上线时,必须关闭服务,不能一边上线,一边让用户可以继续操作,一边产生 ...
- PostgreSQL 列出所有表名和数据库名, 删除session被占用的数据库
https://blog.csdn.net/Michael_Lbs/article/details/57509940
- 【spring基础】spring与jdbc整合详解
先上一段简单示例 public class MyTemplate { private DataSource dataSource; public DataSource getDataSource() ...
- CSS 2. 盒模型|浮动
1.盒模型 盒模型: 在网页中 基本上都会显示一些方方正正的盒子,这种盒子就被我们称为盒模型.重要的属性: width,height,padding,border, margin 盒子模型通过四个边界 ...
- Java 处理 iphone拍照后 图片EXIF属性翻转90度的方法
http://blog.csdn.net/z69183787/article/details/50320821 Java获取照片EXIF信息 http://blog.csdn.net/ghsau/ar ...
- CentOS root用户修改密码
1.root用户修改密码: #passwd -------------------------------- 参考资料: 1.Centos修改root密码:http://blog.163.com/wz ...