【Subsets II】cpp
题目:
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],
[]
]
代码:
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
std::sort(nums.begin(), nums.end());
vector<vector<int> > ret;
vector<int> tmp;
Solution::dfs(ret, nums, , tmp);
return ret;
}
static void dfs(vector<vector<int> >& ret, vector<int>& nums, int index, vector<int>& tmp )
{
if ( index==nums.size() )
{
ret.push_back(tmp);
return;
}
tmp.push_back(nums[index]);
Solution::dfs(ret, nums, index+, tmp);
tmp.pop_back();
if ( !(tmp.size()>= && tmp.back()==nums[index]) )
{
Solution::dfs(ret, nums, index+, tmp);
}
}
};
tips:
大体思路与Subsets类似(http://www.cnblogs.com/xbf9xbf/p/4516802.html)
经过画图推演:dfs向左边走的条件不变;再向右边走时,如果当前节点的最后一个元素与即将要加入的新元素相同,则不往右边走了。因为再往右边走,右边肯定包含左边的重复子集了。
===================================
再补充一个增量构造法的迭代解法,代码如下:
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
std::sort(nums.begin(), nums.end());
vector<vector<int> > ret;
vector<int> none;
ret.push_back(none);
int pureNew = ;
for ( size_t i = ; i < nums.size(); ++i )
{
vector<vector<int> > tmp = ret;
size_t begin = ;
if ( i>= && nums[i]==nums[i-] )
{
begin = ret.size()-pureNew;
}
for ( size_t j = begin; j < tmp.size(); ++j )
{
tmp[j].push_back(nums[i]);
ret.push_back(tmp[j]);
}
pureNew = ret.size() - tmp.size();
}
return ret;
}
};
tips:
增加新元素前,判断nums[i]==nums[i-1]的条件是否成立;如果成立,则增量应该只作用于上一轮新增加的子集上,这样保证没有重复的;这个思路也很简洁。
学习了几个blog的思路:
http://bangbingsyb.blogspot.sg/2014/11/leetcode-subsets-i-ii.html
http://www.cnblogs.com/TenosDoIt/p/3451902.html
http://www.cnblogs.com/yuzhangcmu/p/4211815.html
完毕。
============================================
第二次过这道题,只用dfs的写法。再次画画图,理解一下。
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int>& nums)
{
sort(nums.begin(), nums.end());
vector<vector<int> > ret;
vector<int> tmp;
Solution::dfs(ret, nums, , tmp);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& nums, int index,
vector<int>& tmp)
{
if ( index==nums.size() )
{
ret.push_back(tmp);
return;
}
tmp.push_back(nums[index]);
Solution::dfs(ret, nums, index+, tmp);
tmp.pop_back();
if ( !(tmp.size()> && tmp.back()==nums[index]) )
{
Solution::dfs(ret, nums, index+, tmp);
}
}
};
用深搜的好处就体现出来了。
【Subsets II】cpp的更多相关文章
- 【N-Quens II】cpp
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- leetcode 【 Subsets II 】python 实现
题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. No ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【Path Sum II】cpp
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- 【Unique Binary Search Trees II】cpp
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 【Populating Next Right Pointers in Each Node II】cpp
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
随机推荐
- iOS 微信支付平台集成
https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_5
- crontab的使用说明
网上瞎转载的,仅供参考 名称 : crontab 使用权限 : 所有使用者 使用方式 : crontab file [-u user]-用指定的文件替代目前的crontab. crontab-[-u ...
- sqoop导出工具
sqoop的导出工具是把HDFS上文件中的数据导出到mysql中 mysql中的表 现在在linux上创建一个文件,并把这个文件上传到hdfs上 cat person.txt ,no7, ,no8, ...
- CentOS配置FTP(VSFTPD)
一.vsftp安装篇 # 安装vsftpd yum -y install vsftpd # 启动 service vsftpd start # 开启启动 chkconfig vsftpd on 二.v ...
- 对话框AlertDialog的基本类型与创建
测试代码: 布局: activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...
- seeting菜单界面形成--优化
本文是上一篇文章的优化版: 上文链接地址:http://www.cnblogs.com/zzw1994/p/5016864.html 上文中有很多方法都是过时,并且效率不是很高,主要对JAVA代码进行 ...
- 从PC跳转至wap
<script language="JavaScript">function mobile_device_detect(url){var thisOS=navigato ...
- 压力测试之TCPP
1.下载源码 tpcc-mysql-src.tgz 2.解压 tpcc-mysql-src.tgz 3.安装 [root@DBMysql mysql]# cd /home/mysql/tpcc-mys ...
- SPARK 数据统计程序性能优化。
昨天写完R脚本 没测试就发到博客里, 结果实际运行发现很慢,运行时间在2小时以上, 查看spark控制台, 大量时间消耗在count上, 产生的stage多大70多个 . 分析原因. 1 selec ...
- shell脚本调用spark-sql
为了更方便的查询并产生报表, 需要使用shell脚本调用spark-sql spark/bin/spark-sql --master spark://host:7077 -f ${SQL_FILE} ...