【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 ...
随机推荐
- 自己定义ProgressDialog载入图片
使用系统载入框 mDialog = new ProgressDialog(this); mDialog.setCancelable(true);//能否够被取消 mDialog.setMessage( ...
- 移除WordPress文章图片的宽度和高度属性
通过WordPress自身的媒体上传功能插入到文章的图片,都会默认添加了高度和宽度属性: <img title="使用 Chrome Workspace 进行网站调试 | 倡萌的自留地 ...
- 1-2:CSS3课程入门之结构选择
E:nth-child(n) 表示E父元素中的第n个字节点 p:nth-child(odd){background:red}/*匹配奇数行*/ p:nth-child(even){background ...
- golang手动管理内存
作者:John Graham-Cumming. 原文点击此处.翻译:Lubia Yang(已失效) 前些天我介绍了我们对Lua的使用,implement our new Web Applicati ...
- 再说WCF Data Contract KnownTypeAttribute
WCF 中的序列化是用DataContractSerializer,所有被[DataContract]和[DataMemeber]标记的类和属性会被DataContractSerializer序列化. ...
- 苹果开发之COCOA编程(第三版)下半部分
第十八章:Image和鼠标事件 1.NSResponderNSView继承自NSResponder类.所有的事件处理方法都定义在NSResponder类中.NSResponder申明了如下方法:- ( ...
- iOS_39_触摸解锁
终于效果图: 控制器: // // BeyondViewController.m // 39_触摸解锁 // // Created by beyond on 14-9-17. // Copyright ...
- Asp.Net中判断是否登录,及是否有权限?
不需要在每个页面都做判段, 方法一:只需要做以下处理即可 using System; using System.Collections.Generic; using System.Linq; usin ...
- 九度OJ 1176:树查找 (完全二叉树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5209 解决:2193 题目描述: 有一棵树,输出某一深度的所有节点,有则输出这些节点,无则输出EMPTY.该树是完全二叉树. 输入: 输入 ...
- Notepad++ QuickText 插件的 HTML 配置: \Notepad++\plugins\Config\QuickText.ini
# 缩写的注解 abbr=<abbr title=''>$</abbr> # 覆盖默认的文本方向 bdo=<bdo dir='rtl'>$</bdo> ...