-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSolution.java
More file actions
56 lines (51 loc) · 1.06 KB
/
Solution.java
File metadata and controls
56 lines (51 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package Subsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* User: Danyang
* Date: 1/26/2015
* Time: 13:51
*
* 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],
[]
]
*/
public class Solution {
/**
* Tree
* Notice:
* 1. add at leaves
* @param S
* @return
*/
public List<List<Integer>> subsets(int[] S) {
Arrays.sort(S);
List<List<Integer>> ret = new ArrayList<>();
dfs(S, 0, new ArrayList<>(), ret);
return ret;
}
void dfs(int[] S, int i, List<Integer> cur, List<List<Integer>> ret) {
if(i==S.length)
ret.add(new ArrayList<>(cur));
if(i<S.length) {
dfs(S, i+1, cur, ret);
cur.add(S[i]);
dfs(S, i+1, cur, ret);
cur.remove(cur.size()-1);
}
}
}