9.4 Write a method to return all subsets of a set.

LeetCode上的原题,请参见我之前的博客Subsets 子集合Subsets II 子集合之二

解法一:

class Solution {
public:
vector<vector<int> > getSubsets(vector<int> &S) {
vector<vector<int> > res();
for (int i = ; i < S.size(); ++i) {
int size = res.size();
for (int j = ; j < size; ++j) {
res.push_back(res[j]);
res.back().push_back(S[i]);
}
}
return res;
}
};

解法二:

class Solution {
public:
vector<vector<int> > getSubsets(vector<int> &S) {
vector<vector<int> > res;
vector<int> out;
getSubsetsDFS(S, , out, res);
return res;
}
void getSubsetsDFS(vector<int> &S, int pos, vector<int> &out, vector<vector<int> > &res) {
res.push_back(out);
for (int i = pos; i < S.size(); ++i) {
out.push_back(S[i]);
getSubsetsDFS(S, i + , out ,res);
out.pop_back();
}
}
};

解法三:

class Solution {
public:
vector<vector<int> > getSubsets(vector<int> &S) {
vector<vector<int> > res;
int max = << S.size();
for (int i = ; i < max; ++i) {
vector<int> out = convertIntToSet(S, i);
res.push_back(out);
}
return res;
}
vector<int> convertIntToSet(vector<int> &S, int k) {
vector<int> sub;
int idx = ;
for (int i = k; i > ; i >>= ) {
if ((i & ) == ) {
sub.push_back(S[idx]);
}
++idx;
}
return sub;
}
};

[CareerCup] 9.4 Subsets 子集合的更多相关文章

  1. [LeetCode] Subsets 子集合

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

  2. [LeetCode] 78. Subsets 子集合

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

  3. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. LeetCode 78. Subsets(子集合)

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  5. [LeetCode] 916. Word Subsets 单词子集合

    We are given two arrays A and B of words.  Each word is a string of lowercase letters. Now, say that ...

  6. Java List部分截取,获得指定长度子集合

    subList方法用于获取列表中指定范围的子列表,该列表支持原列表所支持的所有可选操作.返回列表中指定范围的子列表. 语法 subList(int fromIndex, int toIndex) fr ...

  7. [LeetCode] Largest Divisible Subset 最大可整除的子集合

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  8. EF Code First 一对多、多对多关联,如何加载子集合?

    应用场景 先简单描述一下标题的意思:使用 EF Code First 映射配置 Entity 之间的关系,可能是一对多关系,也可能是多对多关系,那如何加载 Entity 下关联的 ICollectio ...

  9. C# 如何从List集合当中取出子集合

    今天项目要求随机从数据库中随机取出若干条数据,放到首页.那么要如何随机取出这个子集合呢?本人向到的方法如下: 1.假设数据量很少,如我数据库中只有10条数据,而我要求随机取出8条.对于这种低数据量,大 ...

随机推荐

  1. 使用eclipse遇到问题:the-package-collides-with-a-type

    相似问题:http://stackoverflow.com/questions/12236909/the-package-collides-with-a-type

  2. 【AdaBoost算法】积分图代码实现

    一.积分图介绍 定义:图像左上方的像素点值的和: 在Adaboost算法中可用于加速计算Haar或MB-LBP特征值,如下图: 二.代码实现 #include <opencv/highgui.h ...

  3. 深入PHP内核之ZVAL

    一.PHP的变量类型 PHP的变量类型有8种: 标准类型:布尔boolen,整型integer,浮点float,字符string 复杂类型:数组array,对象object 特殊类型:资源resour ...

  4. 一个关于group by和having子句的小例子

    表结构: 要求: 查询有多个员工的工资不低于2000的部门编号(就是说如果一个部门的员工大于2000的人数有两个或两个以上就查询出来) sql语句: select [DEPARTMENT_ID],co ...

  5. Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程

    准备篇 一.环境说明: 操作系统:Windows Server 2012 R2 PHP版本:php 5.5.8 MySQL版本:MySQL5.6.15 二.相关软件下载: 1.PHP下载地址: htt ...

  6. 闭包Closures

    所谓闭包,可以理解为一个可以用于函数,参数,返回值处的代码块 import Foundation func isGood(a:Int,b:Int)->Bool{ return a>b; } ...

  7. windows 进程管理器中的内存是什么意思?

    *内存 - 工作集:私人工作集中的内存数量与进程正在使用且可以由其他进程共享的内存数量的总和. *内存 - 峰值工作集:进程所使用的工作集内存的最大数量. *内存 - 工作集增量:进程所使用的工作集内 ...

  8. 【转载】kafka的工作原理

    http://www.ibm.com/developerworks/cn/opensource/os-cn-kafka/index.html 消息队列 消息队列技术是分布式应用间交换信息的一种技术.消 ...

  9. Oracle 分组聚合二种写法,listagg和wmsys.wm_concat

    with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nation ,'Sh ...

  10. [转]jquery-confirm

    本文转自:http://craftpip.github.io/jquery-confirm/ Practical Uses These features can practically be used ...