您的位置:首页 > 编程语言 > Java开发

LeetCode(078) Subsets (Java)

2015-07-02 13:54 483 查看

题目如下:

Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
  ]


分析如下:

标准DFS题目, 所有的subset的解构成一颗树,对这棵树进行DFS遍历,把每个节点都输出即可。

我的代码:

public class Solution {
    void subsetsHelper(int[] nums, List<Integer> each,  List<List<Integer> > arrList, int start , int len) {

        // WRONG : arrList.add(each);   
        // each is only a reference, should use the new operation to instantiate a new array.

        // NOTE: something different from the normal DFS template.
        // NOTE: we need to walk throught every node of the tree, including both the leaf node and the non leaf node, hence we do not use the normal "if (star == len) {... return ;}".
        // NOTE:  if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G<Foo> is a subtype of G<Bar>. This is probably the hardest thing you need to learn about generics, because it goes against our deeply held intuitions.
        // https://docs.oracle.com/javase/tutorial/extra/generics/subtype.html 
        arrList.add(new ArrayList<Integer> (each)); 

        for (int i = start; i < len; ++i) { // DFS
            each.add(nums[i]);
            subsetsHelper(nums, each, arrList, i + 1, len);
            each.remove(each.size() - 1);
        } 
    };

    public List<List<Integer>> subsets(int[] nums) {

        // WRONG: List<List<Integer> > result = new ArrayList<List<Integer> > ();
        // if you had a List<List<Integer>> then you'd be able to add a LinkedList<Integer> to it.
        //But you can't do this for an ArrayList<ArrayList<Integer>>, so the latter can't possibly be a type of List<List<Integer>>.

        //The reason is that generics are not covariant.
        // Consider simpler case:

        // List<Integer> integers = new ArrayList<Integer>();
        // List<Number> numbers = integers; // cannot do this
        // numbers.add(new Float(1337.44));
        // Now List holds a Float, which is certainly bad.

        // Same for your case.

        // List<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>();
        // List<List<Integer>> ll = al; // cannot do this
        // ll.add(new LinkedList<Integer>())
        // Now you have a list ll which holds LinkedList, but the al is declared as a List of ArrayLists.

        List<List<Integer> > result = new ArrayList<List<Integer> > ();
        ArrayList<Integer> each = new ArrayList<Integer>();
        Arrays.sort(nums);
        subsetsHelper(nums, each, result, 0, nums.length);
        return result;
    }
//  public static void main(String[] args) {
//      // TODO Auto-generated method stub
//     Subsets subsets = new Subsets();
//     //int [] arr = new int []{2, 3, 1};
//     int [] arr = new int []{0};
//     List<ArrayList<Integer> > arrArr = subsets.subsets(arr);
//     System.out.println(arrArr.size());
//  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: