-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dfs_2^n.java
58 lines (51 loc) · 1.6 KB
/
AC_dfs_2^n.java
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
57
58
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dfs_2^n.java
* Create Date: 2015-02-08 11:18:01
* Descripton:
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] S) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> cur = new ArrayList<Integer>();
Arrays.sort(S);
dfs(0, res, cur, S);
return res;
}
private void dfs(int dep, List<List<Integer>> res, List<Integer> cur,
int[] S) {
if (dep == S.length) {
res.add(new ArrayList<Integer>(cur));
} else {
int upper = dep;
while (upper >= 0 && upper < S.length - 1 && S[upper] == S[upper + 1]) {
++upper;
}
// no choose
dfs(upper + 1, res, cur, S);
// choose
for (int i = dep; i <= upper; ++i) {
cur.add(new Integer(S[dep]));
dfs(upper + 1, res, cur, S);
}
for (int i = dep; i <= upper; ++i)
cur.remove(cur.size() - 1);
}
}
// debug
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
int[] input = {1, 2, 2};
List<List<Integer>> res = s.subsetsWithDup(input);
for (List<Integer> i : res) {
for (Integer j : i)
System.out.print(j.toString() + ' ');
System.out.println("");
}
}
}