-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ46Permutations.java
More file actions
43 lines (39 loc) · 1.1 KB
/
Q46Permutations.java
File metadata and controls
43 lines (39 loc) · 1.1 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 46.Permutations
*
* @author ahscuml
* @date 2018/9/13
* @time 21:02
*/
public class Q46Permutations {
/**
* 测试函数
*/
public static void main(String[] args) {
int[] nums = {1, 2, 3};
System.out.println(permute(nums).toString());
}
public static List<List<Integer>> permute(int nums[]) {
Arrays.sort(nums);
List<List<Integer>> list = new ArrayList<>();
backTracking(list, new ArrayList<>(), nums);
return list;
}
public static void backTracking(List<List<Integer>> list, List<Integer> templist, int[] nums) {
if (templist.size() == nums.length) {
list.add(new ArrayList<>(templist));
} else {
for (int i =0; i < nums.length; i++) {
if (templist.contains(nums[i])) {
continue;
}
templist.add(nums[i]);
backTracking(list, templist, nums );
templist.remove(templist.size() - 1);
}
}
}
}