forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermute.java
35 lines (29 loc) · 1.02 KB
/
Permute.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
package Algorithms;
import java.util.ArrayList;
import java.util.Arrays;
class Permute {
public static void main(String[] strs) {
Permute per = new Permute();
ArrayList<ArrayList<Integer>> rst = per.permute(new int[]{1,2,3});
}
public ArrayList<ArrayList<Integer>> permute(int[] num) {
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> path = new ArrayList<Integer>();
permuteHelp(num, rst, path);
return rst;
}
public void permuteHelp(int[] num, ArrayList<ArrayList<Integer>> rst, ArrayList<Integer> path) {
if (path.size() == num.length) {
rst.add(new ArrayList<Integer>(path));
return;
}
for (int i = 0; i < num.length; i++) {
if (path.contains(num[i])) {
continue;
}
path.add(num[i]);
permuteHelp(num, rst, path);
path.remove(path.size() - 1);
}
}
}