Subsets------DFS
2015-12-11 22:48
330 查看
Given a set of distinct integers, return all possible subsets.
Have you met this question in a real interview? Yes
Example
If S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Note
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
Have you met this question in a real interview? Yes
Example
If S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Note
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
public class Solution { public ArrayList<ArrayList<Integer>> subsets(int[] num) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(num == null || num.length == 0) { return result; } ArrayList<Integer> list = new ArrayList<Integer>(); Arrays.sort(num); subsetsHelper(result, list, num, 0); return result; } private void subsetsHelper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int[] num, int pos) { result.add(new ArrayList<Integer>(list)); for (int i = pos; i < num.length; i++) { list.add(num[i]); subsetsHelper(result, list, num, i + 1); list.remove(list.size() - 1); } } }
相关文章推荐
- windows 访问ubuntu (ssh-server 安装及使用)
- Scrum Meeting 9-20151211
- Field BSEG-MWSKZ . does not exist in the screen SAPMF05A 0300 Message no. 00349
- UVa 1232 / LA 4108 线段树
- C51单片机学习(一):LED和数码管静态显示
- accept与connect的超时设置
- SDUT AOE关键路径 SPFA求最长路(倒序建路)
- 【jQuery01】添加添加div
- 如何做EL表达式能调用的函数-小例子(转)
- html标签
- 快捷键
- EL表达式详解(转)
- tinyhttpd 剖析
- 阿里巴巴收购《南华早报》
- 阿里巴巴收购《南华早报》
- Java有关def的事项
- My way on Linux - [虚拟化&云计算] - 云计算概述&KVM虚拟化基础
- GIT的使用中的问题处理
- 8成微商都容易忽略这5种营销细节
- 设备驱动的艺术之旅 - Platform 总线的应用