[CareerCup] 9.4 Subsets 子集合
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 子集合的更多相关文章
- [LeetCode] Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- [LeetCode] 78. Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- LeetCode 78. Subsets(子集合)
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- [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 ...
- Java List部分截取,获得指定长度子集合
subList方法用于获取列表中指定范围的子列表,该列表支持原列表所支持的所有可选操作.返回列表中指定范围的子列表. 语法 subList(int fromIndex, int toIndex) fr ...
- [LeetCode] Largest Divisible Subset 最大可整除的子集合
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- EF Code First 一对多、多对多关联,如何加载子集合?
应用场景 先简单描述一下标题的意思:使用 EF Code First 映射配置 Entity 之间的关系,可能是一对多关系,也可能是多对多关系,那如何加载 Entity 下关联的 ICollectio ...
- C# 如何从List集合当中取出子集合
今天项目要求随机从数据库中随机取出若干条数据,放到首页.那么要如何随机取出这个子集合呢?本人向到的方法如下: 1.假设数据量很少,如我数据库中只有10条数据,而我要求随机取出8条.对于这种低数据量,大 ...
随机推荐
- Effective Java 27 Favor generic methods
Static utility methods are particularly good candidates for generification. The type parameter list, ...
- 转 HighCharts笔记之: Bar Chart
最近需要做一些Web图标,研究了几个开源的第三方工具后,最后决定使用HighCharts开发: Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是 ...
- cordova Process finished with exit code -1
安装完cordova之后,创建一个测试项目后,运行报Process finished with exit code -1,经过查找原因,是因为gradle没有安装,在http://www.androi ...
- 运行编译后的程序报错 error while loading shared libraries: lib*.so: cannot open shared object file: No such file or directory
运行编译后的程序报错 error while loading shared libraries: lib*.so: cannot open shared object file: No such f ...
- codeforces C. Triangle
C. Triangle time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- 百度地图的简单使用 ——html js
一.简介 百度地图JavaScript API是一套由JavaScript语言编写的应用程序接口,它能够帮助您在网站中构建功能丰富.交互性强的地图应用,包含了构建地图基本功能的各种接口,提供了诸如本地 ...
- history介绍及bash命令快速调用
在日常工作中,能够快速并准确的使用命令是必不可少的,下面为大家介绍一下其中的小技巧. 一.查找命令历史——history 使用history能够快速的找到之前输入过的命令. # history 大家可 ...
- table边框美化
在<table>里面加代码就可以了. 效果1: <TABLE style="BORDER-RIGHT: #993333 3px dashed; BORDER-TOP: #9 ...
- LAMP编译参数查看
Linux下查看Nginx.Napache.MySQL.PHP的编译参数的命令如下: 1.nginx编译参数:#/usr/local/nginx/sbin/nginx -V2.apache编译参数:# ...
- jemter的使用(三)
前面的文章已经把接口请求.响应等前序工作做好,那么如何施加压力呢,看下面 1.点击线程组,设置线程属性,其中:线程数即并发用户数,ramp-up period是多长时间初始化上面的并发用户数,循环次数 ...