leetcode[91] Subsets II
给定一个数组,返回所有的非重复的可能。例如给定
If S = [1,2,2]
, a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
其实这题类似的有Combination和Subsets。有兴趣的可以看看之前的解题方法。那大概是第70题左右,十天以前做的吧。记得当时用了回溯法。现在这个还是要用到回溯。只是加了判断有重复的子集怎么办。
在回溯的过称中,记录一种可能后就把最后一个pop掉,然后回溯上一个的时候判断是不是和之前判断过的相等了。用一个tmpVal存刚刚pop的数字,如果相等,那就++跳过它。
class Solution {
public:
void dfs91(vector<vector<int> > &ans, vector<int> &tmp, vector<int> S, int start)
{
int tmpVal;
for (int i = start; i < S.size(); ++i)
{
tmp.push_back(S[i]);
dfs91(ans, tmp, S, i+);
ans.push_back(tmp);
tmpVal = tmp[tmp.size()-];
tmp.pop_back();
while(i+ < S.size() && tmpVal == S[i+]) i++; // 跳过和之前pop掉的数字相等的数,因为已经考虑过了
}
}
vector<vector<int> > subsetsWithDup(vector<int> &S)
{
vector<int> empty;
vector<vector<int> > ans(,empty); // 空集总是答案之一
sort(S.begin(), S.end()); // 排好序,才符合非降组合
if (S.size()==) return ans;
dfs91(ans, empty, S, );
return ans;
}
};
leetcode[91] Subsets II的更多相关文章
- [Leetcode Week8]Subsets II
Subsets II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/subsets-ii/description/ Description Given ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90. Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
随机推荐
- SQL 将URL编码转汉字!
原文:SQL 将URL编码转汉字! -- ============================================= -- 作 者: ruijc -- 描 述: 将Url编码转明文字符 ...
- DNSserver内置笔记本
DNS于linuxserver该服务名是named,和named服务相关的软件bind. 周围环境: 系统版本号:VBOX虚拟机centos6.0. 本机内网IP 192.168.2.198. ...
- ArcGIS for Silverlight 地图卷帘
原文:ArcGIS for Silverlight 地图卷帘 ArcGIS 地图卷帘 for Silverlight 地图卷帘,其实就是遮罩的效果,在Silverlight里实现这样的效果,对于熟悉S ...
- php实现和c#一致的DES加密解密
原文:php实现和c#一致的DES加密解密 php实现和c#一致的DES加密解密,可以从网上搜到一大堆,但是测试后发现都没法用.以下正确代码是我经过苦苦才找到的.希望大家在系统整合时能用的上. 注意: ...
- 我国常用的坐标系统WKID列表[转]
原文链接:http://blog.sina.com.cn/s/blog_62f9ffcd0102uw8x.html Geographic Coordinate System 地理坐标 4214 GC ...
- 【Python】Python的urllib模、urllib2模块的网络下载文件
因为需要从一些下载一个页PDF文件.但是需要下载PDF有数百个文件,这是不可能用人工点击下载.只是Python有相关模块,所以写一个程序PDF文件下载,顺便熟悉Python的urllib模块和ulrl ...
- libmsgque官方主页
libmsgque 消息队列(MESSAGE QUEUE)库项目简析 注: 本文如果你已经有linux开发环境 请确保你使用本库时是tag版本号. target=libmsgque-1.0 本项目採用 ...
- 基于Android的ELF PLT/GOT符号和重定向过程ELF Hook实现(by 低端农业代码 2014.10.27)
介绍 技术原因写这篇文章,有两种: 一个是在大多数在线叙述性说明发现PLT/GOT第二十符号重定向过程定向x86的,例<Redirecting functions in shared ELF l ...
- C# 带用户密码访问网络共享
原文:C# 带用户密码访问网络共享 调用WNetUseConnection API 函数详细参数参考:https://msdn.microsoft.com/en-us/library/windows/ ...
- Swiftly语言学习1
单纯值: 1.let常量声明,var声明变量(同时宣布福值,编译器会自己主动判断出类型) var myVariable = 42 myVariable 50 let myConstant = 42 l ...