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

  1. 【leetcode】Subsets II

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  2. 【leetcode】Permutations II (middle)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】课程表 II

    [问题]现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程.例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及 ...

  5. 【Leetcode】【Medium】Subsets II

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

  6. 【leetcode】Subsets (Medium) ☆

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  7. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  8. 【leetcode】N-Queens II

    N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...

  9. 【leetcode】Sort List (middle)

    Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...

随机推荐

  1. 开始使用 Markdown

    (Xee:我最近感觉nyfedit打开有点慢,数据库有点大,试想着用一些其他的方式记录一下学习的过程,才想起了遗忘了很长时间的Markdown,将其分类在HTML下,也是我原本意愿的...) 本文面向 ...

  2. [译]Exploring Angular 1.3: Binding to Directive Controllers

    原文: http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html Angul ...

  3. JS补充

    JavaScript JavaScript 使用那些老旧的实例可能会在 <script> 标签中使用 type="text/javascript".现在已经不必这样做了 ...

  4. 将Centos的yum源更换为阿里云源

    阿里云Linux安装软件镜像源 阿里云是最近新出的一个镜像源.得益与阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源.阿里云Linux安装镜像源地址:http://mirrors.aliyun. ...

  5. linux 中断理解

    1.进程.线程只针对的是应用层,而内核调用.驱动没有这种概念,调用的都是内核调用里相同的函数或变量,所以应用层多个应用操作同个硬件时,特别是要加互斥操作,8250通过cs针脚决定发送数据给哪个串口 2 ...

  6. mysql python image

    连接mysql数据库: cnx = mysql.connector.connect(user='joe', database='test') Connector/Python参数列表 Argument ...

  7. SQL Server 跨数据库查询

    语句 SELECT * FROM 数据库A.dbo.表A a, 数据库B.dbo.表B b WHERE a.field=b.field "DBO"可以省略 如 SELECT * F ...

  8. python 函数式编程工具

    有三个内置函数与列表一起使用时非常有用:filter().map()和reduce(). 1. filter(function, sequence)返回的序列由function(item)结果为真的元 ...

  9. JavaScript获取onclick、onchange等事件值的代码

    这里主要是用到了getAttributeNode()这个方法,它获取的是属性节点,忽略属性和事件的差别,具体示例如下,感兴趣的朋友可以参考下哈希望对大家有所帮助 今天小菜处理下拉菜单级联问题时,想获取 ...

  10. apache2错误日志在哪,可以看到php错误

    在以下路径中 /var/log/apache2/error.log