-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.java
More file actions
49 lines (45 loc) · 1.55 KB
/
Copy pathpermutations.java
File metadata and controls
49 lines (45 loc) · 1.55 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
public class Solution {
private HashMap<Integer, HashSet<Integer>> map;
private int[] num;
private boolean[] used;
private ArrayList<Integer> current;
private ArrayList<ArrayList<Integer>> answer;
public ArrayList<ArrayList<Integer>> permute(int[] num) {
map = new HashMap<Integer, HashSet<Integer>>();
answer = new ArrayList<ArrayList<Integer>>();
this.num = num;
used = new boolean[num.length];
current = new ArrayList<Integer>();
search(0);
return answer;
}
private void search(int p) {
if (p == num.length) {
ArrayList<Integer> newList = new ArrayList<Integer>();
for (Integer i : current) {
newList.add(i);
}
answer.add(newList);
return;
}
for (int i = 0; i < used.length; i++) {
if (!used[i]) {
HashSet<Integer> positions = map.get(num[i]);
if (positions == null || !positions.contains(p)) {
used[i] = true;
if (positions == null) {
positions = new HashSet<Integer>();
}
positions.add(p);
map.put(num[i], positions);
current.add(num[i]);
search(p+1);
current.remove(current.size() - 1);
positions.remove(p);
map.put(num[i], positions);
used[i] = false;
}
}
}
}
}