LeetCode: Subsets 解题报告
Subsets
Given a set of distinct integers, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
SOLUTION 1:
使用九章算法的模板:
递归解决。
1. 先对数组进行排序。
2. 在set中依次取一个数字出来即可,因为我们保持升序,所以不需要取当前Index之前的数字。
TIME: 227 ms
public class Solution {
public List<List<Integer>> subsets(int[] S) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (S == null) {
return ret;
}
Arrays.sort(S);
dfs(S, , new ArrayList<Integer> (), ret);
return ret;
}
public void dfs(int[] S, int index, List<Integer> path, List<List<Integer>> ret) {
ret.add(new ArrayList<Integer>(path));
for (int i = index; i < S.length; i++) {
path.add(S[i]);
dfs(S, i + , path, ret);
path.remove(path.size() - );
}
}
}
SOLUTION 2:
在Solution 1的基础之上,使用Hashmap来记录中间结果,即是以index开始的所有的组合,希望可以加快运行效率,最后时间:
TIME:253 ms.
实际结果与预期反而不一致。原因可能是每次新组装这些解也需要耗费时间
// Solution 3: The memory and recursion.
public List<List<Integer>> subsets(int[] S) {
//
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (S == null) {
return ret;
} Arrays.sort(S);
return dfs3(S, , new HashMap<Integer, List<List<Integer>>>());
} public List<List<Integer>> dfs3(int[] S, int index, HashMap<Integer, List<List<Integer>>> map) {
int len = S.length; if (map.containsKey(index)) {
return map.get(index);
} List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> pathTmp = new ArrayList<Integer>();
ret.add(pathTmp); for (int i = index; i < len; i++) {
List<List<Integer>> left = dfs3(S, i + , map);
for (List<Integer> list: left) {
pathTmp = new ArrayList<Integer>();
pathTmp.add(S[i]);
pathTmp.addAll(list);
ret.add(pathTmp);
}
} map.put(index, ret);
return ret;
}
SOLUTION 3:
相当牛逼的bit解法。基本的想法是,用bit位来表示这一位的number要不要取,第一位有1,0即取和不取2种可能性。所以只要把0到N种可能
都用bit位表示,再把它转化为数字集合,就可以了。
Ref: http://www.fusu.us/2013/07/the-subsets-problem.html
There are many variations of this problem, I will stay on the general problem of finding all subsets of a set. For example if our set is [1, 2, 3] - we would have 8 (2 to the power of 3) subsets: {[], [1], [2], [3], [1, 2], [1, 3], [1, 2, 3], [2, 3]}. So basically our algorithm can't be faster than O(2^n) since we need to go through all possible combinations.
There's a few ways of doing this. I'll mention two ways here - the recursive way, that we've been taught in high schools; and using a bit string.
Using a bit string involves some bit manipulation but the final code can be found easy to understand. The idea is that all the numbers from 0 to 2^n are represented by unique bit strings of n bit width that can be translated into a subset. So for example in the above mentioned array we would have 8 numbers from 0 to 7 inclusive that would have a bit representation that is translated using the bit index as the index of the array.
|
Nr |
Bits |
Combination |
|
0 |
000 |
{} |
|
1 |
001 |
{1} |
|
2 |
010 |
{2} |
|
3 |
011 |
{1, 2} |
|
4 |
100 |
{3} |
|
5 |
101 |
{1, 3} |
|
6 |
110 |
{2, 3} |
|
7 |
111 |
{1, 2, 3} |
public class Solution {
public List<List<Integer>> subsets(int[] S) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (S == null || S.length == ) {
return ret;
}
int len = S.length;
Arrays.sort(S);
// forget to add (long).
long numOfSet = (long)Math.pow(, len);
for (int i = ; i < numOfSet; i++) {
// bug 3: should use tmp - i.
long tmp = i;
ArrayList<Integer> list = new ArrayList<Integer>();
while (tmp != ) {
// bug 2: use error NumberOfTrailingZeros.
int indexOfLast1 = Long.numberOfTrailingZeros(tmp);
list.add(S[indexOfLast1]);
// clear the bit.
tmp ^= ( << indexOfLast1);
}
ret.add(list);
}
return ret;
}
}
性能测试:
1. when SIZE = 19:
Subset with memory record: 14350.0 millisec.
Subset recursion: 2525.0 millisec.
Subset Iterator: 5207.0 millisec.
表明带memeory的性能反而不行。而iterator的性能也不并不如。
2. size再继续加大时,iterator的会出现Heap 溢出的问题,且速度非常非常慢。原因不是太懂。
GITHUB: https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dfs/Subsets.java
LeetCode: Subsets 解题报告的更多相关文章
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】78. Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】916. Word Subsets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- LeetCode C++ 解题报告
自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- LeetCode: isSameTree1 解题报告
isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...
随机推荐
- ASP.NET Core 文件系统
ASP.NET Core 文件系统 静态文件 目录浏览 默认页面 MIME类型配置 实战文件服务器 紧接上一讲 中间件 之后,今天来我们来讲一下关于 ASP.NET Core 中静态文件服务. 什 ...
- 阿里P6大牛给予Java初学者的学习路线建议
Java学习这一部分是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是你是如何学习Java的,能不能给点建议?今天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈每个阶段要 ...
- 20172302 《Java软件结构与数据结构》第三周学习总结
2018年学习总结博客总目录:第一周 第二周 第三周 教材学习内容总结 第五章 队列 1.队列是一种线性集合,其元素从一端加入,从另一端删除:队列元素是按先进先出(FIFO(First in Firs ...
- 报错:[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
报错内容:如下 分析: 遇到这种情况,说明超出了数组的范围 如要插入某组数据,但是这组数据只有10条:但是这里设置为20条.当第11个cell填充数据时就会报错, [__NSArrayI object ...
- v$instance如何生成
参考:http://www.itpub.net/thread-1284858-1-1.html 1.ORACLE 先创建的x$ 表即RDBMS的内部表 2.然后在X$表的基础上创建了GV$ 视图. ...
- 模拟一个带背景的 TPanel
https://www.cnblogs.com/del/archive/2008/09/01/1281345.html
- ASP.NET Web API中通过URI显示实体中的部分字段
有时候我们可能不想显示某个实体中的所有字段.比如客户端发出如下请求: locaohost:43321/api/groups/1/items?fields=idlocaohost:43321/api/g ...
- Detour3.0 win7 64bit下的安装
最近在做API hook相关的东西,用了inline hook后感觉不错,但是查找资料发现inline hook并不稳定 inline hook 的原理是在系统访问一个函数的时候先替换原函数入口处的内 ...
- 你真的会用Gson吗?Gson使用指南(1)
JSON (官网) 是一种文本形式的数据交换格式,它比XML更轻量.比二进制容易阅读和编写,调式也更加方便.其重要性不言而喻.解析和生成的方式很多,Java中最常用的类库有:JSON-Java.Gso ...
- YUV 4:2:0 格式和YUV411格式区别
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/coloriy/article/details/6668447 MPEG 储存的 YU(Cb)V(Cr ...