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. leetcode47. Permutations II

    leetcode47. Permutations II 题意: 给定可能包含重复的数字的集合,返回所有可能的唯一排列. 思路: 这题全排列两种写法. 用hash表记录已经visit的数字,虽然看起来很 ...

  2. 将mnist数据集存储到本地文件

    参考文章: http://www.csuldw.com/2016/02/25/2016-02-25-machine-learning-MNIST-dataset/ import numpy as np ...

  3. 擦亮自己的眼睛去看SQLServer之谈谈锁机制

    http://www.cnblogs.com/yueyue_jwfm/category/292724.html

  4. Arcgis license 服务无法启动的解决问题

    来自:http://blog.csdn.net/u013719339/article/details/51240312 1.检查服务开没开.打开资源管理器然后按照下面就出现了.也可以打开运行——ser ...

  5. 仿LOL项目开发第二天

    仿LOL项目开发第二天 by草帽 接着上节来讲,上节更新还没开始写代码逻辑,今天我们补充完整. 我们找到VersionManager脚本里面的CheckVersion方法: 首先我们想到检测版本,需要 ...

  6. EF DBContext中DbSet中Hashset添加对象后,DataGrid UI没有刷新的问题

    使用EF4/5添加数据库视图生成DBContext,如果数据表/对象之间有M-N对应关系,EF对自动添加引用类集合,是Hashset类型.由于不是ObservableCellection类型,在引用类 ...

  7. Neo4j 使用cypher语言进行查询

    Neo4j是一个Java开发的图数据库,它将结构化数据存储在网络(从数学角度叫做图)上而不是表中.相对于关系数据库来说,图数据库善于处理大量复杂.互连接.低结构化的数据,这些数据变化迅速,需要频繁的查 ...

  8. jetty7.6运行struts2项目问题解决

    运行struts2项目报错:报错1:11:56:51,400  WARN Dispatcher:68 - Could not find action or result: /credit_public ...

  9. qsort(),sort()排序函数

    一.qsort()函数 功 能: 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp ...

  10. TextView子类的常用属性

    TextView常见的子类包括EditText,Button,CheckBox, RadioButton等. 1.EditText EditText继承自TextView,因此TextView所有属性 ...