Subsets 解答
Question
Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Solution 1 -- BFS
Because it's required that subset must be non-descending order, we can sort input array first. We can write out the solution space tree.
For example, input is [1,2,3]
[]
/ | \
[1] [2] [3]
/ \ |
[1,2] [1,3] [2,3]
If element of last level is [i, j, .., k], then we could traverse elements from [k + 1, .., last] to add one element to construct complete answers.
BFS can be easily implemented to solve this problem. Time complexity is size of solution space tree, ie, Cn0 + Cn1 + Cn2 + .. + Cnn/2
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
int length = nums.length;
List<List<Integer>> results = new ArrayList<List<Integer>>();
List<List<Integer>> current = new ArrayList<List<Integer>>();
results.add(new ArrayList<Integer>());
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < length; i++) {
map.put(nums[i], i);
List<Integer> tmp = new ArrayList<Integer>();
tmp.add(nums[i]);
current.add(tmp);
results.add(tmp);
}
while (current.size() > 0) {
List<List<Integer>> next = new ArrayList<List<Integer>>();
int l = current.size();
for (int i = 0; i < l; i++) {
List<Integer> currentList = current.get(i);
int ll = currentList.size();
int last = currentList.get(ll - 1);
int index = map.get(last);
for (int j = index + 1; j < length; j++) {
// Note: create a new object
List<Integer> newList = new ArrayList<Integer>(currentList);
newList.add(nums[j]);
next.add(newList);
results.add(newList);
}
}
current = next;
}
return results;
}
}
Solution 2 -- DFS
We can transfer this problem to be list all combinations from number 0 to nums.length
And we can get combinations of each size by DFS. Same time complexity as solution 1.
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
int length = nums.length;
List<List<Integer>> results = new ArrayList<List<Integer>>();
results.add(new ArrayList<Integer>());
for (int i = 1; i <= length; i++) {
dfs(nums, 0, results, new ArrayList<Integer>(), i);
}
return results;
}
private void dfs(int[] nums, int start, List<List<Integer>> results, List<Integer> list, int level) {
if (list.size() == level)
results.add(new ArrayList<Integer>(list));
for (int i = start; i < nums.length; i++) {
list.add(nums[i]);
dfs(nums, i + 1, results, list, level);
list.remove(list.size() - 1);
}
}
}
Subsets 解答的更多相关文章
- Subsets II 解答
Question Given a collection of integers that might contain duplicates, nums, return all possible sub ...
- 深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- 刷题78. Subsets
一.题目说明 题目78. Subsets,给一列整数,求所有可能的子集.题目难度是Medium! 二.我的解答 这个题目,前面做过一个类似的,相当于求闭包: 刷题22. Generate Parent ...
- Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
随机推荐
- 做一个有理想的IT人
前段时间一直以来都在思考生命的价值的问题,一直在想人的一生的追求是什么.在这个物欲横流的社会,对人的价值的定义只是在财富积累的多少,这个是大多数人所认为的.但人的一生顶多百年,百年之后这些虚荣划归为尘 ...
- linux中patch命令 -p 选项
patch命令和diff命令是linux打补丁的成对命令,diff 负责生产xxxxx.patch文件,patch命令负责将补丁打到要修改的源码上.但是patch命令的参数-p很容易使人迷惑,因为对- ...
- HDOJ-1013 Digital Roots
http://acm.hdu.edu.cn/showproblem.php?pid=1013 1.给出一个整数,求每一位上的数字之和 2.若求出的和大于1位,则对该和继续执行第1步,直至和的位数为1 ...
- Java学习作业(14.4.21)
前三次作业都是基础语法.真的好水啊.从这次开始记录. 1.编写Java程序,把当前目录下扩展名为txt的文件的扩展名全部更名为back. import java.io.*; import java.l ...
- linux之getcwd函数解析
[lingyun@localhost getcwd]$ cat getcwd.c /********************************************************** ...
- Uninstall or Disable Java on a Mac
You can run Java apps in two ways. The first is to run Java applets inside your Web browser with a p ...
- [Spring入门学习笔记][Spring的AOP原理]
AOP是什么? 面向切面编程 软件工程有一个基本原则叫做“关注点分离”(Concern Separation),通俗的理解就是不同的问题交给不同的部分去解决,每部分专注于解决自己的问题.这年头互联网也 ...
- VS删除未使用的命名空间
把鼠标光变放在Using+命名空间区域,右键——组织 using->移除未使用的 using
- (转)ASP.Net 学习路线
入门篇 1.学习面向对象(OOP)的编程思想 许多高级语言都是面向对象的编程,.NET也不例外.如果您第一次接触面向对象的编程,就必须理解类.对象.字段.属性.方法和事件.封装.继承和多态性.重载.重 ...
- 关于自定义UICollectionViewLayout的一点个人理解<一>
自定义UICollectionView,主要会用到以下几个方法: - (void)prepareLayout; 第一次加载layout.刷新layout.以及- (BOOL)shouldInvalid ...