【Lintcode】017.Subsets
题目:
题解:
Solution 1 ()
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res{{}};
sort(S.begin(), S.end());
for (int i = ; i < S.size(); ++i) {
int size = res.size();
for (int j = ; j < size; ++j) {
vector<int> instance = res[j];
instance.push_back(S[i]);
res.push_back(instance);
}
}
return res;
}
};
Solution 1.2
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res(, vector<int>());
sort(S.begin(), S.end());
for (int i = ; i < S.size(); i++) {
int n = res.size();
for (int j = ; j < n; j++) {
res.push_back(res[j]);
res.back().push_back(S[i]);
}
}
return res;
}
};
Solution 2 ()
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res;
vector<int> v;
sort(S.begin(), S.end());
dfs(res, S, v, );
return res;
}
void dfs(vector<vector<int> > &res, vector<int> S, vector<int> &v, int pos) {
res.push_back(v);
for (int i = pos; i < S.size(); ++i) {
v.push_back(S[i]);
dfs(res, S, v, i + );
v.pop_back();
}
}
};
Bit Manipulation
This is the most clever solution that I have seen. The idea is that to give all the possible subsets, we just need to exhaust all the possible combinations of the numbers. And each number has only two possibilities: either in or not in a subset. And this can be represented using a bit.
There is also another a way to visualize this idea. That is, if we use the above example, 1 appears once in every two consecutive subsets, 2 appears twice in every four consecutive subsets, and 3 appears four times in every eight subsets, shown in the following (initially the 8 subsets are all empty):
[], [], [], [], [], [], [], []
[], [1], [], [1], [], [1], [], [1]
[], [1], [2], [1, 2], [], [1], [2], [1, 2]
[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]
Solution 3 ()
class Solution {
public:
vector<vector<int>> subsets(vector<int>& S) {
sort(S.begin(), S.end());
int num_subset = pow(, S.size());
vector<vector<int> > res(num_subset, vector<int>());
for (int i = ; i < S.size(); i++) {
for (int j = ; j < num_subset; j++) {
if ((j >> i) & ) {
res[j].push_back(S[i]);
}
}
}
return res;
}
};
【Lintcode】017.Subsets的更多相关文章
- 【Lintcode】018.Subsets II
题目: Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each ...
- 【CF660E】Different Subsets For All Tuples 结论题
[CF660E]Different Subsets For All Tuples 题意:对于所有长度为n,每个数为1,2...m的序列,求出每个序列的本质不同的子序列的数目之和.(多个原序列可以有相同 ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【medium】78. Subsets
求集合不重复的子集: 下面python的写法很好啊! class Solution(object): def subsets(self, nums): """ :type ...
- 【LeetCode】78. Subsets (2 solutions)
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 【Lintcode】074.First Bad Version
题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version ...
- 【LeetCode】90.Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...
随机推荐
- gulp 静态资源版本控制
package.json { "name": "gulp", "version": "0.0.1", "des ...
- php解码“&#”编码的中文用函数html_entity_decode()
遇到类似 ' 这种编码的字,我们可以用html_entity_decode()函数来解码. html_entity_decode() 函数把 HTML 实体转换为字符. 语法 html_entity_ ...
- Mysql 5.7.18 加密连接mysql_ssl_rsa_setup
MySQL 5.7.18 下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64. ...
- Mysql 免密码登录,修改密码及忘记密码操作
----免密码登陆 方式一 my.cnf增加[client]标签 [client] user="root" password="你的密码" 单对定义不同的客户端 ...
- 给定一颗完全二叉树,给每一层添加上next的指针,从左边指向右边
给你机会发出声音,但是不给你机会证明高层的决定是错的 RT: 时间复杂度O(n) 空间复杂度O(1) 原理就是有指针指向父节点和当前的节点,左孩子必指向右孩子,右孩子必指向父节点的下一个节点的左孩子 ...
- XP,32/64位Win7,32/64位Win10系统【电脑城版】
本系统是10月最新完整版本的Windows10 安装版镜像,Win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为Win10 Edge浏览器中国默认主页和搜索引擎,系 ...
- WPF DataGrid 获取当前行某列值
[0]是指当前行第1列的单元格位置 注意:DataRowView要求必须引用System.Data命名空间 方法一: DataRowView mySelectedElement = (DataRowV ...
- 获取网站的BaseURL
//get base URL var _urlstr = window.location.href; if (_urlstr.indexOf("? ...
- Linux 进程状态 说明
Linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程序,就必然会产生很多的进程,而每个进程会有不同的状态. 在下文将对进程的 R.S.D.T.Z.X 六种状态做个说明. 进程状态: S ...
- pjax简单实例
ajax缺点是破坏了浏览器的前进后退,因为ajax的请求不会留在历史记录中.pjax就不一样了,pjax被解释成ajax+pushState的封装,因为它把ajax的请求写入历史记录,并反映在地址栏, ...