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],
[]
]

解法一:

遍历S.size()位数的所有二进制数,1代表对应位置的元素在集合中,0代表不在。

一共2^n种情况。

详细步骤参照Subsets II

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > result;
int size = S.size();
for(int i = ; i < pow(2.0, size); i ++)
{//2^size subsets
vector<int> cur;
int tag = i;
for(int j = size-; j >= ; j --)
{//for each subset, the binary presentation has size digits
if(tag% == )
cur.push_back(S[j]);
tag >>= ;
if(!tag)
break;
}
sort(cur.begin(), cur.end());
result.push_back(cur);
}
return result;
}
};

解法二:

遍历所有元素,记当前元素为S[i]

遍历当前所有获得的子集,记为ret[j]

将S[i]加入ret[j],即构成了一个新子集。

详细步骤参照Subsets II

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > ret;
vector<int> empty;
ret.push_back(empty);
for(int i = ; i < S.size(); i ++)
{
int size = ret.size();
for(int j = ; j < size; j ++)
{
vector<int> newset = ret[j];
newset.push_back(S[i]);
ret.push_back(newset);
}
}
return ret;
}
};

【LeetCode】78. Subsets (2 solutions)的更多相关文章

  1. 【LeetCode】78. Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  2. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  3. 【LeetCode】90. Subsets II (2 solutions)

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

  4. 【一天一道LeetCode】#78. Subsets

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【medium】78. Subsets

    求集合不重复的子集: 下面python的写法很好啊! class Solution(object): def subsets(self, nums): """ :type ...

  6. 【LeetCode】77. Combinations (2 solutions)

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...  ...

  7. 【LeetCode】90.Subsets II

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

  8. 【LeetCode】18. 4Sum (2 solutions)

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  9. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

随机推荐

  1. PAT甲级1018. Public Bike Management

    PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...

  2. HelloWorld 和相关设置

    写这篇文章的初衷很简单,就是想再一次证明 IntelliJ IDEA 对于 Java 开发人员来说,确实比 eclipse 要好用得多,鉴于目前市面上关于 IntelliJ IDEA 的教程比较少,叙 ...

  3. 腾讯PHP工程师面试题两份

    试题一: PHP开发工程师笔试试卷 姓名:__________ 一.PHP开发部分 1.合并两个数组有几种方式,试比较它们的异同 2.请写一个函数来检查用户提交的数据是否为整数(不区分数据类型,可以为 ...

  4. 【java失业择业中】失业第四天:准备面试

    1.jQuery基础 学好jquery的一个基础条件是学好css层叠样式,因为很多时候这2个是一块配合使用的. 页面中很多需要jquery实现的效果只是通过jquery的选择器,选中要操作的元素,添加 ...

  5. Oracle简单脚本演示样例

    Oracle简单脚本演示样例 1.添加表 --改动日期:2014.09.21 --改动人:易小群 --改动内容:新增採购支付情况表 DECLARE VC_STR           VARCHAR2( ...

  6. printf回到上一行开头以及回到本行开头的方法

    回到上一行开头 #include <stdio.h> #include <unistd.h> int main(void) { ; ){ printf("%d\n&q ...

  7. 判断openfire用户的状态

    /** * 判断openfire用户的状态 * 说明 :必须要 openfire加载 presence 插件,同时设置任何人都可以访问 * /status?jid=user1@my.openfire. ...

  8. 理解JavaScript中的事件流

    原文地址:http://my.oschina.net/sevenhdu/blog/332014 目录[-] 事件冒泡 事件捕获 DOM事件流 当浏览器发展到第四代时(IE4和Netscape Comm ...

  9. 如何使用Omnifocus做时间管理 4 多项目管理

    这一篇和大家分享作为一个管理者,可能遇到的时间管理挑战和如何用omnifocus应对这些挑战. 张经理在一家IT公司上班,从程序员开始打拼了5年,凭借权威的技术能力当上了项目经理,同时管理三个项目,苦 ...

  10. MRIcro tutorial -- mricro 教程

      MRIcro tutorial 参考网址:http://www.mccauslandcenter.sc.edu/mricro/mricron/ http://www.cabiatl.com/mri ...