题目:

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的更多相关文章

  1. 【N-Quens II】cpp

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. leetcode 【 Subsets II 】python 实现

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

  3. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  4. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  5. 【Unique Paths II】cpp

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  6. 【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 ...

  7. 【Unique Binary Search Trees II】cpp

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  8. 【Populating Next Right Pointers in Each Node II】cpp

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  9. 【Binary Tree Level Order Traversal II 】cpp

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

随机推荐

  1. banner淡出效果

    <div class="banner"> <div class="ban"></div> <ul class=&quo ...

  2. Connect to a Windows PC from Ubuntu via Remote Desktop Connection

    http://www.7tutorials.com/connecting-windows-remote-desktop-ubuntu A useful feature of Windows is be ...

  3. 为什么swing不适合做桌面软件

    http://www.zhihu.com/question/19608871 我最近几年做的项目清一色的都是HTML5了,这篇<基于HTML5的电信网管3D机房监控应用>供参考,HTML5 ...

  4. Oracle中存储过程与函数的区别

    Oracle 获取信息一般用function 修改数据用存储过程(需要执行commit命令)

  5. Nginx+Tomcat+Memcached集群

    Tomcat集群session同步方案有以下几种方式: 使用tomcat自带的cluster方式,多个tomcat间自动实时复制session信息,配置起来很简单.但这个方案的效率比较低,在大并发下表 ...

  6. Windows下安装redis,并与PHP使用

    一.在windows下安装redis: redis的官方网站下载地址:http://redis.io/download 进入以上网址之后,请见以下的图片操作下载redis: 第一步: 第二步:在对应的 ...

  7. CSS 设置TABLE 表格 边框

    /*table列表 合并边框设置*/ .tablelist { border-collapse:collapse; } /*table列表 设置边框宽度及颜色*/ .tablelist td { bo ...

  8. 第十二章 管理类型(In .net4.5) 之 操作字符串

    1. 概述 本章包括 字符串基本操作 以及 查找.遍历.格式化字符串. 2. 主要内容 2.1 在.net平台中使用字符串 C#中,string是用来保存文本信息的.是一个被当做值类型使用的引用类型. ...

  9. Python核心编程--学习笔记--6--序列(下)列表、元组

    11 列表 类似于C语言的数组,但是列表可以包含不同类型的任意对象.列表是可变类型. 创建列表——手动赋值.工厂函数: >>> aList = [12, 'abc'] >> ...

  10. static关键字的作用

    static可以用来定义静态成员变量.静态函数.静态代码块. 静态成员变量的语法特点 定义方法:在成员变量前面加上static class Person{ static int i; //静态成员变量 ...