题目

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. 4月8日学习笔记(js基础)

    <script>标签放在<body>和<head> 放在 <head></head> 里的会比放在 <body></bod ...

  2. C#调用WinAPI及窗口过程

    C#调用WINAPI及Windows窗口消息的发与送 最近在做一个餐饮项目(C#Winform),其中有一块是做点菜宝接口的对接,点菜宝的厂商提供了一个WX.exe的驱动程序,这个驱动程序无直接打开, ...

  3. opencv 手写选择题阅卷 (四)Android端 手机应用开发

    opencv 手写选择题阅卷 (四)Android 手机应用开发 在PC端把代码调通以后开始开发Android 手机应用,因为主要功能代码为C++代码,所以需要通过NDK编译,JAVA通过JNI方式调 ...

  4. bzoj 1023: [SHOI2008]cactus仙人掌图

    这道题是我做的第一道仙人掌DP,小小纪念一下…… 仙人掌DP就是环上的点环状DP,树上的点树上DP.就是说,做一遍DFS,DFS的过程中处理出环,环上的点先不DP,先把这些换上的点的后继点都处理出来, ...

  5. css圆角 四边投影

    -moz-border-radius: 30px;-webkit-border-radius: 30px; border-radius:30px; -webkit-box-shadow:0 0 10p ...

  6. 窗体位置设置StartPosition属性

    有如下选项,分别含义如下: CenterParent                    窗体在其父窗体中居中.      CenterScreen                    窗体在当前 ...

  7. Demo学习: Dialogs Anonymous Callback

    Dialogs\Dialogs Anonymous Callback 窗体回调函数使用. 1. 标准回调函数 ShowMessage(const Msg: string; CallBack: TUni ...

  8. Linux恢复删除文件

    一.介绍extundelete 1.extundelete的文件恢复工具,该工具最给力的一点就是支持ext3/ext4双格式分区恢复. 2. 在实际线上恢复过程中,切勿将extundelete安装到你 ...

  9. AVL树的python实现

    AVL树是带有平衡条件的二叉查找树,一般要求每个节点的左子树和右子树的高度最多差1(空树的高度定义为-1). 在高度为h的AVL树中,最少的节点数S(h)由S(h)=S(h-1)+S(h-2)+1得出 ...

  10. Python urllib2多进程共享cookies

    如果想多个进程共享同一个cookies,不用每个进程都重新登录,可以就cookies保存到一个文件,然后多个进程直接共享一个锁来实现 1.一个进程登录完成后,把cookies保存到一个文件里面 sel ...