Leetcode: Subsets & SubsetsII
Subsets Description:
Given a set of distinct integers, S, 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 S = [1,2,3]
, a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(),S.end());
vector<vector<int> > result; vector<int> comb;
result.push_back(comb); for(int i=;i<S.size();i++)
{
int nows = result.size();
for(int j=;j<nows;++j)
{
comb = result[j];
comb.push_back(S[i]);
result.push_back(comb);
}
} return result;
}
};
SubsetsII Description:
Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2]
, a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
分析:这个题目和上面的唯一区别就在于集合中有相同的元素, 然后再遇到后面是相同元素的时候,添加到子集中产生新子集时,相当于要选择往里面添加几个这个元素。
这里直接用跟上面一样的方法,只是再最后把它们放到set中,去除重复元素,然后再放回vector中返回。这种方法好像有点萌蠢。。 但是呢,根据上面的分析,还有一种
做法,就是将vector中所有元素遍历一遍,放到map中,map中key就是元素值,value是重复的个数,然后就用这个map开始同样的过程,只是产生新的子集的时候
,根据value值,分别产生加不同数量的该元素。
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) { sort(S.begin(),S.end());
set<vector<int> > setres;
vector<vector<int> > results; vector<int> comb;
results.push_back(comb); for(int i=;i<S.size();i++)
{
int nows = results.size();
for(int j=;j<nows;++j)
{
comb = results[j];
comb.push_back(S[i]);
results.push_back(comb);
}
}
setres.insert(results.begin(),results.end());
results.assign(setres.begin(),setres.end()); return results;
}
};
Leetcode: Subsets & SubsetsII的更多相关文章
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [leetcode]Subsets II @ Python
原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...
- [LeetCode] Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- leetcode — subsets
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- [leetcode]Subsets @ Python
原题地址:https://oj.leetcode.com/problems/subsets/ 题意:枚举所有子集. 解题思路:碰到这种问题,一律dfs. 代码: class Solution: # @ ...
- [Leetcode] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
随机推荐
- UVa 10223 - How many nodes ?
称号:气你一个整数n,问:多少节点可以产生n不同的二叉树. 分析:数论,卡特兰数.根据定义,你可以. 说明:请参阅http://blog.csdn.net/mobius_strip/article/d ...
- error C3130: 内部编译器错误: 未能将插入的代码块写入PDB
近期编译cocos2d-x的test突然出现这个错误,又一次编译也无法解决. 一般出现这个错误是两个原因:一个是磁盘空间不足,还有一个是项目太大导致pdb文件太大,无法继续写入. 原本cocos2d- ...
- PHP移动互联网的发展票据(6)——MySQL召回数据库基础架构[1]
原文地址 :http://www.php100.com/html/php/api/2014/0326/6707.html 一.数据类型 1.整型 数据类型 存储空间 说明 取值范围 TINYINT 1 ...
- GitFlow使用说明
———————安装--------------- $ git clone --recursive git://github.com/nvie/gitflow.git $ cd gitflow $ [s ...
- DevExpress asp.net 导出Excel 自动开启迅雷问题,默认保存为aspx页面
目前采取曲线救国策略: 利用MVC ..... <dx:ASPxGridView ID="ASPxGridView1" runat="server" Au ...
- 出现Deprecated: Function ereg_replace() is deprecated in 的原因及解决方法
在 php5.3环境下运行oscommerce,常常会出现Deprecated: Function ereg() is deprecated in...和Deprecated: Function er ...
- MongoDB的C#驱动
MongoDB的C#驱动基本使用 MongoDB的官方C#驱动可以通过这个链接得到.链接提供了.msi和.zip两种方式获取驱动dll文件. 通过这篇文章来介绍C#驱动的基本数据库连接,增删改查操作. ...
- 【转】NuGet的安装与使用
学习了一段时间的MVC,今天想自己尝试初步搭建一个MVC框架,结果新建MVC4.0(MVC3.0同样)项目时,弹出一个错误提示框,如下图.上网一搜,说是要安装一个第三方组件NuGet.刚接触MVC,更 ...
- php调用webservice报错Class 'SoapClient' not found
原文:php调用webservice报错Class 'SoapClient' not found php在调用webservice时,报告如下类似错误: ( ! ) Fatal error: Clas ...
- leetcode第18题--Letter Combinations of a Phone Number
Problem: Given a digit string, return all possible letter combinations that the number could represe ...