题目:

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. WindowsPhone使用HtmlAgilityPack解析HTML

    NuGet里添加HtmlAgilityPack的引用 然后wp上使用必须添加本地 C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libr ...

  2. 逻辑运算符||和| 、&&和&的区别

    ||和| .&&和&的区别 这里以&&和&为例.或与之一直 1.&和&&都可以用作逻辑与的运算符,表示逻辑与(and),当运算符 ...

  3. Aspose.cell.dll的使用,导excel表

    using System; using System.Web; using EF; using Newtonsoft.Json; using System.Collections.Generic; u ...

  4. windows phone上下文菜单ContextMenu的使用示例

    新建一个WP项目,命名为contextmenu,然后往界面拖入一个ListBox控件listBox1,接着切换到XAML代码界面设置其属性,代码如下 <ListBox Height=" ...

  5. require.js js模块化方案

    一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载.下面的网页代 ...

  6. Centos 7.1+CDH5.7.2全部署流程

    前期准备: JDK环境 版本:jdk-8u101-linux-x64.rpm 下载地址:oracle官网 mysql rpm包:http://dev.mysql.com/get/Downloads/M ...

  7. Php中正则小结(一)

    一.概念 语法模式类似perl.表达式必须用分隔符闭合,比如一个正斜杠(/). 分隔符可以是任意非字母非数字,除反斜杠(\)和空字节之外的非空白ascii字符 如果分隔符 在表达式中使用,需要使用反斜 ...

  8. 基于jQuery编写的横向自适应幻灯片切换特效

    基于jQuery编写的横向自适应幻灯片切换特效 全屏自适应jquery焦点图切换特效,在IE6这个蛋疼的浏览器兼容性问题上得到了和谐,兼容IE6. 适用浏览器:IE6.IE7.IE8.360.Fire ...

  9. .NET软件汉化小实例

    Author:KillerLegend Date:2014.6.18 From:http://www.cnblogs.com/killerlegend/p/3795577.html 好的,今天我们来汉 ...

  10. 新成员!Visual Studio Code --跨平台的开发工具(支持OSX, Linux 和 Windows)

    原文出处:新成员!Visual Studio Code --跨平台的开发工具(支持OSX, Linux 和 Windows) 这是我的文章备份  http://www.dotblogs.com.tw/ ...