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()); // } }
相关文章推荐
- Spring定时任务的几种实现
- java反射——对内部类的处理
- Java过滤器与SpringMVC拦截器之间的关系与区别
- java 指定打印机 进行打印
- java的学习之路
- webService之(五)Spring与CXF整合
- Java中浮点数的存储格式
- 各种数据库再spring中的配置(转载)
- git在eclipse中的配置
- 使用eclipse,如何快速进入选中文件所在的文件夹
- eclipse 启动指定虚拟机路径
- myeclipse如何恢复已删除的文件和代码
- 停止一个java的线程执行
- Java Web中Kaptcha实现验证码
- springMVC接受表单数据
- java总结(之二)--容器的深入研究
- javax.persistence.EntityNotFoundException: Unable to find XXXX with id 0 问题原因
- Android Studio导入eclipse工程(引用多个其它工程)
- java 防止xss攻击 通过filter的方法(推荐)
- [Java]单向链表 ,循环链表的简单使用方法