【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
思路:回溯法是肯定的了,我用的深度优先,先把输入的S排序,这样当考虑了前面的数字后,后面的讨论中就不需要再考虑该数字了。需要对每次的子序列判断是否已经重复出现。
代码AC了,但是800ms太慢,贴边过的。
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int>> ans;
vector<int> null;
ans.push_back(null);
if(S.size() == )
return ans;
sort(S.begin(), S.end());
DFS(ans, S);
return ans;
}
void DFS(vector<vector<int>> &ans, vector<int> S)
{
if(S.empty())
return;
vector<int> vPreLen = ans.back();
while(!S.empty())
{
vector<int> v = vPreLen;
int cur = S[];
v.push_back(cur);
S.erase(S.begin());
if(!isalreadyhave(ans, v))
{
ans.push_back(v);
DFS(ans, S);
}
}
return;
}
bool isalreadyhave(vector<vector<int>> ans, vector<int> v)
{
for(int i = ; i < ans.size(); i++)
{
if(v == ans[i])
return true;
}
return false;
}
};
大神的思路:48ms
在压入答案的时候就保证不会重复,把重复的部分如(5,5,5)看做一个特殊的数,压入时可以压入1个、2个、3个。
把S排序后,按顺序从第一个到最后一个得到该数字加入后,可以得到的新的子序列。每次加入一个新的数字后,新的数字会使得所有之前已经压入的子序列产生新的子序列。
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
vector<vector<int> > totalset = {{}};
sort(S.begin(),S.end());
for(int i=; i<S.size();){
int count = ; // num of elements are the same
while(count + i<S.size() && S[count+i]==S[i]) count++;
int previousN = totalset.size();
for(int k=; k<previousN; k++){
vector<int> instance = totalset[k];
for(int j=; j<count; j++){
instance.push_back(S[i]);
totalset.push_back(instance);
}
}
i += count;
}
return totalset;
}
};
【leetcode】Subsets II (middle) ☆的更多相关文章
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【leetcode】Permutations II (middle)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】课程表 II
[问题]现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程.例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及 ...
- 【Leetcode】【Medium】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【leetcode】N-Queens II
N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
随机推荐
- [整理]Android开发(二)-Weather App
private class WeatherData{ private String _weatherDescription; private Integer _currentTemperature; ...
- fastJSON☞JSONParameters☞时区的修改☞时间最后有一个"Z"
why... 为什么会有这个问题; 由于近期用到需要将数据序列化... 最终选择了fastJSON(版本为1.)来实现. 但是发现了一个问题,表中有一个dateTime类型的字段, 本来数据库中存入的 ...
- mytbatis小问题
使用mybatis出现以下异常 SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, Postgr ...
- HDU 5033 Building
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5033 解题报告:在一条x轴上有n个建筑物,每个建筑物有一个高度h,然后现在有q次查询,查询的内容是假设 ...
- iOS跳转到另一个程序
我这里只是写了部分东西,如果想看更加详细的,请点击原文链接. 原文链接:http://blog.csdn.net/likendsl/article/details/7553605 原则上iOS的沙 ...
- C++变量对比java变量所占内存
C++ char 1 short int 2 int 4 long int 8 float 4 double 8 long double 8 下面是计算程序: #include<math.h&g ...
- Codeforces 724 E Goods transportation
Description 有 \(n\) 个点,每个点有一个入流和出流,每个点与编号比它大的点连边,容量为 \(c\) ,求最大流. Sol DP. 这种特殊的图可以DP,把最大流转化成最小割. 最小割 ...
- 6 HandlerDescriptor 处理程序描述类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- VB中 ByRef与ByVal区别
函数调用的参数传递有"值传递"和"引用传递"两种传递方式.如果采用"值传递",在函数内部改变了参数的值,主调程序的对应变量的值不会改变:如果 ...
- nginx 下 bootstrap fa 字体异常问题
server { listen 8082; # server_name 192.168.16.88; # root /home/ywt/workspace/kuF/web/statics; # aut ...