题目:

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:

  1. [
  2. [3],
  3. [1],
  4. [2],
  5. [1,2,3],
  6. [1,3],
  7. [2,3],
  8. [1,2],
  9. []
  10. ]

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

题解:

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

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

  1. public class Solution {
  2. public ArrayList<ArrayList<Integer>> subsets(int[] S) {
  3. ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
  4. if(S == null || S.length == 0)
  5. return result;
  6. ArrayList<Integer> list = new ArrayList<Integer>();
  7. Arrays.sort(S);
  8. helper(result, list, S, 0);
  9. return result;
  10. }
  11.  
  12. private void helper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int[] S, int pos){
  13. result.add(new ArrayList<Integer>(list));
  14.  
  15. for(int i = pos; i < S.length; i ++){
  16. list.add(S[i]);
  17. helper(result, list, S, ++pos);
  18. list.remove(list.size() - 1);
  19. }
  20. }
  21. }

Updates:

  1. public class Solution {
  2. public List<List<Integer>> subsets(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. if(nums == null || nums.length == 0)
  5. return res;
  6. Arrays.sort(nums);
  7. ArrayList<Integer> list = new ArrayList<>();
  8. dfs(res, list, nums, 0);
  9. return res;
  10. }
  11.  
  12. private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums, int pos) {
  13. res.add(new ArrayList<Integer>(list));
  14.  
  15. for(int i = pos; i < nums.length; i++) {
  16. list.add(nums[i]);
  17. dfs(res, list, nums, ++pos);
  18. list.remove(list.size() - 1);
  19. }
  20. }
  21. }

Update:

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

  1. public class Solution {
  2. public List<List<Integer>> subsets(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. if(nums == null || nums.length == 0)
  5. return res;
  6. Arrays.sort(nums);
  7. ArrayList<Integer> list = new ArrayList<>();
  8. dfs(res, list, nums, 0);
  9. return res;
  10. }
  11.  
  12. private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums, int pos) {
  13. res.add(new ArrayList<Integer>(list));
  14.  
  15. for(int i = pos; i < nums.length; i++) {
  16. list.add(nums[i]);
  17. dfs(res, list, nums, i + 1);
  18. list.remove(list.size() - 1);
  19. }
  20. }
  21. }

二刷:

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

这道题目我们也是使用跟上一题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)

  1. public class Solution {
  2. public List<List<Integer>> subsets(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. if (nums == null || nums.length == 0) {
  5. return res;
  6. }
  7. Arrays.sort(nums);
  8. List<Integer> list = new ArrayList<>();
  9. getSubsets(res, list, nums, 0);
  10. return res;
  11. }
  12.  
  13. private void getSubsets(List<List<Integer>> res, List<Integer> list, int[] nums, int pos) {
  14. res.add(new ArrayList<Integer>(list));
  15. for (int i = pos; i < nums.length; i++) {
  16. list.add(nums[i]);
  17. getSubsets(res, list, nums, i + 1);
  18. list.remove(list.size() - 1);
  19. }
  20. }
  21. }

三刷:

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

Java:

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

  1. class Solution {
  2. public List<List<Integer>> subsets(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. if (nums == null || nums.length == 0) return res;
  5. subsets(res, nums, new ArrayList<Integer>(), 0);
  6. return res;
  7. }
  8.  
  9. private void subsets(List<List<Integer>> res, int[] nums, List<Integer> list, int idx) {
  10. res.add(new ArrayList<>(list));
  11.  
  12. for (int i = idx; i < nums.length; i++) {
  13. list.add(nums[i]);
  14. subsets(res, nums, list, i + 1);
  15. list.remove(list.size() - 1);
  16. }
  17. }
  18. }

  

测试:

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. javascript中的省市级联效果

    学习javascript的时候都遇到过这样的需求,不仅是省市,还有其他的一些场景,看看关键的代码有哪些吧. <head runat="server"> <titl ...

  2. javascript 柯里化

    先看一下代码 function add(){ var sum=0; for(var i=0;i<arguments.length;i++){ sum+=arguments[i]; } retur ...

  3. 微软职位内部推荐-Principal Dev Manager

    微软近期Open的职位: Title: Principal Dev Manager Location: Beijing The R&D of Shared Data Platform at S ...

  4. C语言基础:指针类型与指针和数组、字符串的关系

    //指针变量就是用来存储地址的,只能存储地址 格式:  int  *p;  这个p为指针变量:指针变量占8个字节 类型是用来说明这个指针指向的类型: 比如上边的int代表这个指针变量会指向int类型的 ...

  5. HTTP 错误 500.21 - Internal Server ErrorHTTP

    应用程序“DEFAULT WEB SITE/WINDRP_TB/TBFXWS”中的服务器错误Internet Information Services 7.5错误摘要HTTP 错误 500.21 - ...

  6. SQL语言笔记

      字符串用单引号',判断用单等号=,两个单引号''转义为一个单引号' 不等号是<> 不区分大小写 []括起来的要不是关键字,要不是非法变量,比如空格隔起来的变量   创建与删除数据库 - ...

  7. HDU 4101 Ali and Baba

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4101 一看之下以为是博弈,后来分析才知道只是搜索题. 首先,我们需要从值为-1的位置由内向外搜索一次, ...

  8. Matlab中@与函数调用

    function m f=@(x) x^2; y(f,3); function y(f,x) disp(num2str(f(x))); end end 函数调用另一个函数的时候,把另一个函数名作为参数 ...

  9. 牛顿迭代法实现平方根函数sqrt

    转自利用牛顿迭代法自己写平方根函数sqrt 给定一个正数a,不用库函数求其平方根. 设其平方根为x,则有x2=a,即x2-a=0.设函数f(x)= x2-a,则可得图示红色的函数曲线.在曲线上任取一点 ...

  10. 国内最大的 Node.js 社区将 New Relic 的监控产品换成了 OneAPM

    国内最知名的 CNode 社区把 New Relic 的监控产品换成了 OneAPM .难道 APM 的老大 New Relic 已经被 OneAPM 超越? 毋庸置疑,在全球应用性能管理 SaaS ...