【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 ...
随机推荐
- Laravel 5.4---后端数据分页和前端显示分页结果
后端数据(Eloquent 模型)分页 事先建立好Eloquent 模型和Controller 还有 前台的View.可以参考我之前的文章:Laravel建站03--建立前台文章列表和文章详情 在co ...
- IOS研究之网络编程(二)-Cocoa Streams使用具体解释
本文以及相关的系列文章是我总结的iOS网络开发方面的知识点,本文是第二篇,主要分析了Cocoa Streams中的几个重要类 Cocoa Streams实际上是Objective-C对CFNet ...
- java中Executor、ExecutorService、ThreadPoolExecutor介绍
源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes the given c ...
- eclipse-jee版配置tomcat
Eclipse作为一款优秀的java开发开源IDE,集成了许多优秀的开发控件.下来我就如何安装eclipse及插件进行说明: 一.JDK安装 JDK是作为整个java的核心,包括运行环境,编译工具 ...
- 小tip: DOM appendHTML实现及insertAdjacentHTML
一.无人不识君 据说今天是邓丽君奶奶会见马克思的日子,所谓“无人不识君”就多了份“无人不识邓丽君”之意. JS中有很多基本DOM方法,例如createElement, parentNode等,其中,a ...
- Java过滤特殊字符
Java正则表达式过滤 1.Java过滤特殊字符的正则表达式----转载 java过滤特殊字符的正则表达式[转载] 2010-08-05 11:06 Java过滤特殊字符的正则表达式 关键字: j ...
- 在mac下搭建java开发环境
刚刚从windows系统转到使用mac系统.感觉不是特别熟悉,须要一定的适应时间. 以下简介一下mac下搭建主要的java开发环境. 1.安装jdk 安装jdk1.7后,发现不须要进行环境变量配置,直 ...
- NDK以及C语言基础语法(一)
一.什么是NDK? Native Development Kit (本地开发工具包): NDK中提供了一系列的工具,帮助我们快速开发C/C++的动态库,并能自动将so文件和java文件一起打包成apk ...
- npm ERR! fatal: unable to connect to github.com
https://blog.csdn.net/baidu_30809315/article/details/86520093 git config --global url."https:// ...
- cf-341C Iahub and Permutations
C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...