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: ...
随机推荐
- opencart修改后台文件夹名
在使用opencart进行二次开发时,若需要修改后台目录的文件夹名是可以操作的.具体步骤如下: 1.将网站后台文件夹名字改成opencartadmin 2.在该文件夹下找到config.php文件如图 ...
- MVC 增加统一异常处理机制
原文地址:http://www.cnblogs.com/leoo2sk/archive/2008/11/05/1326655.html 摘要 本文将对“MVC公告发布系统”的发布公告功能添加 ...
- mysql 中文配置(转)
Dos下连接mysql后,运行一下几项就可以插入中文了: SET character_set_client = gbk; SET character_set_connection = gbk; SET ...
- UVa133.The Dole Queue
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Makefile中使用foreach生成一类规则
CSDN上,有朋友发帖问了这样一个问题(我按自己的理解翻译一下): 当前目录下有四个静态库文件: liba.a libb.a libc.a libd.a.现在想将它们做成一个动态库libp.so. ...
- A星算法
没有采用二叉堆算法优化, 学习了几天终于搞除了一个demo, 这个列子如果点击按钮生成的方块大小不正确,可以先设置下预设调成相应的大小 只能上下左右走 using UnityEngine; usi ...
- Ubuntu14.04配置cuda-convnet
转载请注明:http://blog.csdn.net/stdcoutzyx/article/details/39722999 在上一个链接中,我配置了cuda,有强大的GPU,自然不能暴殄天物,让资源 ...
- Git 笔记一 Git简介
git 笔记一 什么是版本控制 所谓版本控制就是记录对文件的修改记录,这样以后就能回退到需要的 版本.比如你对一段代码进行了几次修改,有几次修改不想要了,如果 使用了版本控制,就可以回退到未做这些修改 ...
- 自适应SimpsonSimpson积分
- jquery怎么获取URL的参数
function request(paras) { var url = location.href; var paraString = ur ...