题目

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. sql2000下如何新建并使用dbml

    默认新建的dbml只是支持sql2005及其以上版本. 但是现在是sql2000怎么办?我要是想要用linq to sql 的? 解决方案如下: 1首先打开cmd,在其中cd到sqlmetal.exe ...

  2. NodeJS文件读取:感恩常在--抓把糖果,愉悦客人

    通过上一篇文章“NodeJS服务器:一行代码 = 一个的HTTP服务器”,我们已经开启了NodeJS之旅,开发了一个监听在8000端口的HTTP服务器,虽然功能很简单,但是,已经让我们感受到用Node ...

  3. Oracle数据类型对应Java类型

    SQL数据类型 JDBC类型代码 标准的Java类型 Oracle扩展的Java类型   1.0标准的JDBC类型:     CHAR java.sql.Types.CHAR java.lang.St ...

  4. 笔试面试题-寻找Coder

    请设计一个高效算法,再给定的字符串数组中,找到包含"Coder"的字符串(不区分大小写),并将其作为一个新的数组返回.结果字符串的顺序按照"Coder"出现的次 ...

  5. 写在十年 2007-09-15 (写给L之三)

    你知道吗? 那种时间很远,但心很近的感觉.   时间已经远去了十年, 但亲切的感觉依然清晰可见,   无论时光远去了十年,二十年,三十年, 永远…… 它已经植根,在心间……

  6. 【实习记】2014-08-19升级vim配置YouCompleteMe并debug的过程+qtcreator有语言包没法换语言

        做了个小项目,有空闲可以做点事了. 偶然查资料看见YouCompleteMe的鼎鼎大名. 演示demo <img src="http://i.imgur.com/0OP4ood ...

  7. color 颜色代码 android res/values/colors.xml

    <?xml version="1.0" encoding="utf-8"?> <resources> <color name=&q ...

  8. firefox 自定义快捷键

    firefox 更新到44或45,发现原来的更改快捷键的扩展没了!!!

  9. ES6学习笔记(三)

    ES6加强了对Unicode的支持,并且扩展了字符串对象. 1.字符的Unicode表示法 JavaScript允许采用\uxxxx形式表示一个字符,其中"xxxx"表示字符的码点 ...

  10. 修正 phpcmsv9 VIP过期日期为1970

    打开 phpcms/modules/member/member.php 找到 $form_overdudate = form::date('info[overduedate]', date('Y-m- ...