题目:

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],
[]
]

链接: http://leetcode.com/problems/subsets/

题解:

求数组子数组。先把数组排序,之后就可以使用DFS,维护一个递增的position,递归后要backtracking。

Time Complexity - O(n * 2n), Space Complexity - O(n)

public class Solution {
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(S == null || S.length == 0)
return result;
ArrayList<Integer> list = new ArrayList<Integer>();
Arrays.sort(S);
helper(result, list, S, 0);
return result;
} private void helper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int[] S, int pos){
result.add(new ArrayList<Integer>(list)); for(int i = pos; i < S.length; i ++){
list.add(S[i]);
helper(result, list, S, ++pos);
list.remove(list.size() - 1);
}
}
}

Updates:

public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length == 0)
return res;
Arrays.sort(nums);
ArrayList<Integer> list = new ArrayList<>();
dfs(res, list, nums, 0);
return res;
} private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums, int pos) {
res.add(new ArrayList<Integer>(list)); for(int i = pos; i < nums.length; i++) {
list.add(nums[i]);
dfs(res, list, nums, ++pos);
list.remove(list.size() - 1);
}
}
}

Update:

为什么以前总写成++pos? i + 1就可以了

public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length == 0)
return res;
Arrays.sort(nums);
ArrayList<Integer> list = new ArrayList<>();
dfs(res, list, nums, 0);
return res;
} private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums, int pos) {
res.add(new ArrayList<Integer>(list)); for(int i = pos; i < nums.length; i++) {
list.add(nums[i]);
dfs(res, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}

二刷:

发现自己以前不懂装懂糊弄过去了好多题...我勒个去。

这道题目我们也是使用跟上一题combination类似的方法。

  1. 这里我们根据题意首先要对数组排个序
  2. 构造一个辅助函数getSubsets来进行DFS和backtracking, 同时这个辅助函数还要有一个pos来控制遍历的位置,我们先pass 0 进去。
  3. 每次进入getSubsets我们都直接往结果集中加入一个当前List的新副本
  4. 接下来从pos开始遍历整个数组,每次进入新一层dfs的时候pass 新的pos =  i + 1,这样就能保证结果中的顺序是从小到大

Java:

Time Complexity - O(n!), Space Complexity (n2)

public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
Arrays.sort(nums);
List<Integer> list = new ArrayList<>();
getSubsets(res, list, nums, 0);
return res;
} private void getSubsets(List<List<Integer>> res, List<Integer> list, int[] nums, int pos) {
res.add(new ArrayList<Integer>(list));
for (int i = pos; i < nums.length; i++) {
list.add(nums[i]);
getSubsets(res, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}

三刷:

下次还需要研究Bit Manipulation 以及 iterative的写法。

Java:

Time Complexity - O(n * 2n), Space Complexity (2n)

class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) return res;
subsets(res, nums, new ArrayList<Integer>(), 0);
return res;
} private void subsets(List<List<Integer>> res, int[] nums, List<Integer> list, int idx) {
res.add(new ArrayList<>(list)); for (int i = idx; i < nums.length; i++) {
list.add(nums[i]);
subsets(res, nums, list, i + 1);
list.remove(list.size() - 1);
}
}
}

  

测试:

Reference:

https://leetcode.com/discuss/72498/simple-iteration-no-recursion-no-twiddling-explanation

https://leetcode.com/discuss/25696/simple-java-solution-with-for-each-loops

https://leetcode.com/discuss/29631/java-subsets-solution

https://leetcode.com/discuss/46668/recursive-iterative-manipulation-solutions-explanations

https://leetcode.com/discuss/9213/my-solution-using-bit-manipulation

http://www.cnblogs.com/springfor/p/3879830.html

http://www.cnblogs.com/zhuli19901106/p/3492515.html

http://www.1point3acres.com/bbs/thread-117602-1-1.html

78. Subsets的更多相关文章

  1. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

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

  2. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  3. 刷题78. Subsets

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

  4. [LeetCode] 78. Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  5. Leetcode#78 Subsets

    原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...

  6. 78 Subsets(求子集Medium)

    题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...

  7. LeetCode OJ 78. Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  8. LeetCode 78. Subsets(子集合)

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

  9. 78. Subsets(中等,集合的子集,经典问题 DFS)

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

随机推荐

  1. openerp模块收藏 基于Lodop的报表打印模块(转载)

    基于Lodop的报表打印模块 原文:http://shine-it.net/index.php/topic,7397.0.html 前段时间写了个小模块,来解决OE中报表打印不方便的问题.借鉴了 @b ...

  2. 1103. Integer Factorization (30)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  3. c++迭代器(iterator)详解

    1. 迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型.(1) 每种容器类型都定义了自己的迭代器类型,如vector:vector<int>::iterator iter ...

  4. xk01创建供应商保存的时候,提示错误“科目800001已经存在”

    解决方法:

  5. 初见IOS的UI之:UI控件的属性frame bounds center 和transform

    这些属性,内部都是结构体:CGRect CGPoint CGFloat 背景知识:所有的控件都是view的子类,屏幕就是一个大的view:每个view都有个viewController,它是view的 ...

  6. C语言基础:两个变量交换值的方法

    学习任何语言基础时,两个数值得交换是必须掌握的,下面是3种不同的方式(c语言) 方法一:利用数学的计算技巧 #include <stdio.h> int main() { , b = ; ...

  7. HTML & XML 转义字符

    HTML & XML 转义字符 HTML中<, >,&等有特殊含义,(前两个字符用于链接签,&用于转义),不能直接使用.使用这三个字符时,应使用它们的转义序列,如下 ...

  8. NeatUpload 同时选择并上传多个文件

    neatUpload是asp.net 中可以同时上传多个文件的控件,主页:http://neatupload.codeplex.com/. 效果如下图(显示有点不正常...): 使用步骤: 1. 在a ...

  9. Asp.Net原理Version1.0

    Asp.Net原理Version2.0 Asp.Net原理Version3.0_页面声明周期

  10. 【BZOJ】【1662】/【POJ】【3252】 【USACO 2006 Nov】Round Number

    数位DP 同上一题Windy数 预处理求个组合数 然后同样的方法,这次是记录一下0和1的个数然后搞搞 Orz cxlove /************************************* ...