题目

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],
[]
] 题解:
一个思路就是套用combination的方法,其实combination那道题就是在求不同n下的subset,这里其实是要求一个集合罢了。
例如k=3,n=1,用combination那道题的方法求得集合是[[1], [2], [3]];
k=3, n=2, 用combination那道题的方法求得集合是[[1, 2], [1, 3], [2, 3]]
k=3, n=3, 用combination那道题的方法求得集合是[[1,2,3]]
所以上述3个集合外加一个空集不就是
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
么?
只需要在combination的外面加个循环即可。 代码如下:
 1 public static void dfs(int[] S, int start, int len, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
 2         if(item.size()==len){
 3             res.add(new ArrayList<Integer>(item));
 4             return;
 5         }
 6         for(int i=start; i<S.length;i++){
 7             item.add(S[i]);
 8             dfs(S, i+1, len, item, res);
 9             item.remove(item.size()-1);
         }
 
     }
     
     public static ArrayList<ArrayList<Integer>> subsets(int[] S) {
         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
         ArrayList<Integer> item = new ArrayList<Integer>();
         if(S.length==0||S==null)
             return res;
         
         Arrays.sort(S);
         for(int len = 1; len<= S.length; len++)
             dfs(S,0,len,item,res);
             
         res.add(new ArrayList<Integer>());
         
         return res;
     }
Reference:http://blog.csdn.net/worldwindjp/article/details/23300545

底下是另外一个很精炼的算法。
 1  public static void dfs(int[] S, int start, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
 2         for(int i=start; i<S.length;i++){
 3             item.add(S[i]);
 4             res.add(new ArrayList<Integer>(item));
 5             dfs(S,i+1, item,res);
 6             item.remove(item.size()-1);
 7         }
 8 
 9     }
     
     public static ArrayList<ArrayList<Integer>> subsets(int[] S) {
         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
         ArrayList<Integer> item = new ArrayList<Integer>();
         if(S.length==0||S==null)
             return res;
         
         Arrays.sort(S);
         dfs(S,0,item,res);
         res.add(new ArrayList<Integer>());
         
         return res;
     }
 Reference:http://blog.csdn.net/u011095253/article/details/9158397

Subset leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. Subset II leetcode java

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

  3. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  4. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  5. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  6. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  7. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. 【目录】LeetCode Java实现

    这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...

  9. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

随机推荐

  1. (转)实战Memcached缓存系统(6)Memcached CAS的多线程程序实例

    1. 源程序 package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAdd ...

  2. SQL 存储过程加事务的使用

    create proc USP_CUTTING_TATABLET_PULL_FINISH ( @name NVARCHAR(20) ) as SET XACT_ABORT ON--设置全盘回滚 BEG ...

  3. 转载:邮箱正则表达式Comparing E-mail Address Validating Regular Expressions

    Comparing E-mail Address Validating Regular Expressions Updated: 2/3/2012 Summary This page compares ...

  4. WinForm窗体间如何传值的几种方法

    (转) 窗体间传递数据,无论是父窗体操作子窗体,还是子窗体操作符窗体,有以下几种方式: 公共静态变量: 使用共有属性: 使用委托与事件: 通过构造函数把主窗体传递到从窗体中: 一.通过静态变量 特点: ...

  5. 让nginx支持PHP

    MAC为例 1.安装nginx brew install nginx 2.查找nginx配置文件在什么地方 find /|grep nginx.conf 3.修改配置文件nginx.conf cd / ...

  6. mac下安装pcntl

     Now you need to find out what version of PHP is installed on OSX $ php -vPHP 5.3.10 with Suhosin-Pa ...

  7. php 批量替换html标签的实例代码

    php批量替换html标签的实例代码分享.   1.把html元素全部去掉,或者保留某几个html标签 <?php $text = '<p>Test paragraph.</p ...

  8. 给虚拟机中的CentOS7配置固定ip

    在虚拟机中安装完了CentOS7之后,使用了DHCP来获取ip,vmware的网络连接使用了NAT模式.但是在把Linux设置为固定ip地址后,虚拟机里的linux可以ping通全网段的ip地址,但是 ...

  9. The content of element type "sqlMapConfig" is incomplete,

    The content of element type "sqlMapConfig" is incomplete, it must match "(properties? ...

  10. onclick控制元素显示与隐藏时,点击第一次无反应的原因

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...