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 + Cn+ 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 解答的更多相关文章

  1. Subsets II 解答

    Question Given a collection of integers that might contain duplicates, nums, return all possible sub ...

  2. 深度优先搜索算法(DFS)以及leetCode的subsets II

    深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...

  3. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  4. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  5. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  6. 刷题78. Subsets

    一.题目说明 题目78. Subsets,给一列整数,求所有可能的子集.题目难度是Medium! 二.我的解答 这个题目,前面做过一个类似的,相当于求闭包: 刷题22. Generate Parent ...

  7. Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  8. Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  9. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

随机推荐

  1. 选择排序(Selection Sort)

    选择排序就是在选择数组元素上做文章,关键是如何选择?选择的标准是什么?选择之后放在哪?所有这些都是选择排序的问题. 选择排序算法中,通常会有以下操作: 从数组第一个元素开始. 遍历整个数组,找到最小的 ...

  2. mycat实例(1)

    2016二月 22 置原 MyCat - 使用篇(1) 分类:数据库分库分表(Mycat等) (1126) (1) 数据库路由中间件MyCat - 使用篇(1) 基本概念 直接介绍概念太枯燥了,还是拿 ...

  3. 利用PHPExcel转Excel柱形图

    这在附还有一个转柱形图的效果及代码. 原PHP报表效果: 转成Excel后的效果: 附上代码: <? php /** * PHPExcel * * Copyright (C) 2006 - 20 ...

  4. 10. 混淆矩阵、总体分类精度、Kappa系数

    一.前言 表征分类精度的指标有很多,其中最常用的就是利用混淆矩阵.总体分类精度以及Kappa系数. 其中混淆矩阵能够很清楚的看到每个地物正确分类的个数以及被错分的类别和个数.但是,混淆矩阵并不能一眼就 ...

  5. 关于.net 对.manifest清单文件查找缓存的猜想

    问题背景: winform调用unity web player 插件. 按如下操作: ,编译后会生成.manifest清单文件: 通过清单内容可以看出程序在运行时是按照以上信息来查找ActiveX控件 ...

  6. 代码先行-log4Net初体验

    1.安装 从http://logging.apache.org/log4net/download_log4net.cgi下载编译好的log4Net包并解压. 找到 bin\net\4.0\releas ...

  7. 修改sqlserver2008中表的schema

    schema类似命名空间,相同schema中不能有同样的表名,不用schema下可以有相同的表名 修改schema的方法: 在数据库的 安全性->架构 中添加一个新的架构 找到要修改的表,右击设 ...

  8. Know Thy Complexities!

    http://bigocheatsheet.com/ Hi there!  This webpage covers the space and time Big-O complexities of c ...

  9. SQL Server -SET ANSI_NULLS

    当ANSI_NULLS 为ON时,遵循SQL92的标准,只能使用IS NULL 来判断值是否为NULL, 而不能使用=或<>来与NULL做比较,任何值包括NULL值与NULL值做=或< ...

  10. nginx配置文件(反向代理+集群+动静分离)

    1.nginx纯反向代理配置(nginx.conf): #user nobody;worker_processes 4;error_log logs/error.log info;pid logs/n ...