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. SSH框架之一详解maven搭建多模块项目

    闲来无事,思量着自己搭建一个ssh框架,一来回顾熟悉一下ssh的内容,hibernate还就没用过了,生疏了都.二来整合一下,将其他掌握的和正在学习的框架核技术糅合到一起,就当是做一个demo练手了. ...

  2. enmo_day_06

    RAC Data Guard (DG) EMC NAS SAN 双活 数据完整性 约束 : 主键 : 非空 且 唯一 非空 : 唯一 : 外键 : 检查 : DISABLE, ENABLE VALID ...

  3. Oracle创建,删除用户与表空间

    1.创建表空间与用户 a:创建数据表空间 create tablespace user_data logging datafile 'D:\oracle\product\10.2.0\oradata\ ...

  4. c# windows编程控件学习-1

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Centos上的安装openoffice+unoconv+swftools (转)

    ############################## #    swftools的安装     # ############################## 1.安装所需的库和组件 yum ...

  6. plist文件Boolean类型读写方法

    http://blog.csdn.net/a6472953/article/details/7659505   转 1.读取plist文件中的Boolean类型的字段值时,要先把它转为NSNumber ...

  7. Python学习路程day13

    JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. ...

  8. hdu 2084

    ps:这道题...是DP题..所以我去看了百度一些东西,才知道了什么是状态方程,状态转移方程.. 做的第一个DP题,然后TLE一次.贴上TLE的代码: #include "stdio.h&q ...

  9. Magento文件系统目录结构

    magento │  .htaccess│  cron.php //系统cron程序,修改 linux的cron运行,加入magento的一些定时处理│  cron.sh│  favicon.ico ...

  10. mysql控制台操作

    显示表结构  : show create table  table_name 复制表:insert into table_name1 select * from table_name2