Subsets

Given a set of distinct integers, 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,3], a solution is:

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

思想: 顺序读,取前面的每个子集,把该位置数放后面作为新的子集。
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > vec(1);
for(size_t id = 0; id < S.size(); ++id) {
int n = vec.size();
while(n-- > 0) {
vec.push_back(vec[n]);
vec.back().push_back(S[id]);
}
}
return vec;
}
};

Subsets II

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],
[]
]
思想: 排序后,按照 1 的方法。但是若前面的数字与本数字相同,则只读取含有前面数字的每个子集,把自身放在后面作为一个新的子集。
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > vec(1);
size_t prePos, endTag;
prePos = endTag = 0;
for(size_t id = 0; id < S.size(); ++id) {
if(id > 0 && S[id] != S[id-1]) endTag = 0;
else endTag = prePos;
size_t n = vec.size();
prePos = n;
while(n > endTag) {
--n;
vec.push_back(vec[n]);
vec.back().push_back(S[id]);
}
}
return vec;
}
};
												

42. Subsets && Subsets II的更多相关文章

  1. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  2. leetcode -day31 Subsets I II

    1.  Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a ...

  3. Subsets I&&II——经典题

    Subsets I Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a s ...

  4. LeetCode Subsets I& II——递归

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

  5. Subsets,Subsets II

    一.Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a s ...

  6. &lt;LeetCode OJ&gt; 78 / 90 Subsets (I / II)

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

  7. 二分查找 BestCoder Round #42 1002 Gunner II

    题目传送门 /* 题意:查询x的id,每次前排的树倒下 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) */ #include <cstdi ...

  8. [Swift]LeetCode78. 子集 | Subsets

    Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...

  9. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

随机推荐

  1. (BFS)poj2935-Basic Wall Maze

    题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...

  2. HTTP 304

    304 的标准解释是:Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档).服务器告诉客户,原来缓冲的 ...

  3. jQueryMobile控件之ListView

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. linux系统下php安装mbstring扩展的二种方法

    .执行 复制代码代码如下: yum install php-mbstring 2. 修改php.ini (这一步非常重要, 部分lxadmin版本无法自动修改) 复制代码代码如下: echo ‘ext ...

  5. 从数学角度看最大期望(EM)算法 II

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2015/3/13 对于隐变量只有有限个取值(比如$N$个)的情况,我们可以将隐变量表示为${z_j} = [{z_{j ...

  6. Linux TC基于CBQ队列的流量管理范例

    参考了TC的很多文档,自己也整理了一篇配置记录.在实际使用过程中效果还不错,在此分享给大家以备参考.环境:局域网规模不是很大40多台机器. NAT共享上网(内网:eth0 外网:eth2)CBQ是通过 ...

  7. 这些 Git 技能够你用一年了

    这些 Git 技能够你用一年了 原文出处: Pyper 用git有一年了,下面是我这一年来的git使用总结,覆盖了日常使用中绝大多数的场景.嗯,至少是够用一年了,整理出来分享给大家,不明白的地方可以回 ...

  8. java中的final关键词

    参考资料: http://www.cnblogs.com/dolphin0520/p/3736238.html final是个修饰词,可以修饰类.方法.变量. 1. 修饰类 修饰类,就表示这个类不能被 ...

  9. select 通过jq赋值

    <select name="F_YSBAQLX" onchange="selectvalue()" id="lista" prompt ...

  10. UVA-11297 Census(线段树套线段树)

    题目大意:二维空间点修改,询问矩形区域最值. 题目分析:还是比较简单的. 代码如下: # include<iostream> # include<cstdio> # inclu ...