【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 ...
随机推荐
- java生成4个不同的随机数
package lianxi; import java.util.Random; public class suijishu { public static void main(String[] ar ...
- 当局部变量遇上全局变量——extern及花括号用法举例
请阅读以下代码并说出它的输出结果. #include <stdio.h> ; int foo() { ; { extern int val; printf("val_foo = ...
- jansen字符串转换为xml格式
/// <summary> /// json字符串转换为Xml对象 /// </summary> /// <param name="sJson"> ...
- linux下proc目录详解
proc/pid记录了什么cd /proc/之后,你会发现很多的目录和文件,今天首先来介绍的就是那些以数字命名的目录--它们就是linux中的进程号,每当你创建一个进程时,里面就会动态更新多出一个名称 ...
- c#中判断对象为空的几种方式(字符串等)
(1)先了解几个与空类型相关的关键字和对象 Null : 关键字表示不引用任何对象的空引用,它是所有引用类型变量的默认值,在2.0版本之前也就只有引用变量类型可以为null,如(string a=n ...
- 微软ASP.NET MVC 学习地址
微软ASP.NET MVC4.0学习地址:http://www.asp.net/mvc
- JavaScript计算日期间隔以及结果错误(少一天)的解决方法
下面的代码是之前从网上某个地方COPY下来的,之前一直用着,前段时间DateDiff()方法突然出问题了,输入两个日期2015-10-01 和 2015-10-02之后,计算出来的日期是0!如果只有几 ...
- [leetcode]_Binary Tree Inorder Traversal
题目:二叉树的中序遍历. 思路:用递归来写中序遍历非常简单.但是题目直接挑衅说,----->"Recursive solution is trivial".好吧.谁怕谁小狗. ...
- Mac 安装 Tomcat
默认mac已经安装好java jdk-----/Library/Java/JavaVirtualMachines 1. http://tomcat.apache.org/download-70.cgi ...
- PHP计算某个目录大小的方法
用PHP来计算某个目录大小的方法. PHP CURL session COOKIE 可以调用系统命令,还可以这样: <?php function dirsize($dir) { @$dh ...