-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
75 lines (66 loc) · 2.1 KB
/
Permutations.java
File metadata and controls
75 lines (66 loc) · 2.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package medium;
import static util.Utils.exch;
import static util.Utils.printArray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* ClassName: Permutations
* @author chenyiAlone
* Create Time: 2018/12/14 09:13:31
* Description: No.46 全排列
*/
public class Permutations {
public List<List<Integer>> permute(int[] nums) {
Integer[] ansInt = new Integer[nums.length];
for (int i = 0; i < nums.length; i++) {
ansInt[i] = Integer.valueOf(nums[i]);
}
List<List<Integer>> ans = new ArrayList<List<Integer>>();
permute(ansInt, 0, ans);
return ans;
}
public static void permute(Integer[] nums, int p, List<List<Integer>> list) {
if (p == nums.length - 1) {
List<Integer> ans = new ArrayList(Arrays.asList(nums));
System.out.println(ans);
list.add(ans);
}
for (int i = p; i < nums.length; i++) {
Integer temp = nums[p];
nums[p] = nums[i];
nums[i] = temp;
permute(nums, p + 1, list);
temp = nums[p];
nums[p] = nums[i];
nums[i] = temp;
}
}
/* public List<List<Integer>> permute(int[] nums) {
return null;
}
public static void perm(int[] nums) {
perm(nums, 0, nums.length);
}
public static void perm(int[] nums, int p, int q) {
if (p == q - 1)
printArray(nums);
else {
for (int i = p; i < q; i++) {
exch(nums, p, i);
perm(nums, p + 1, q);
exch(nums, p, i);
}
}
}*/
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5};
// new Permutations().permute(nums);
System.out.println(new Permutations().permute(nums));
// Arrays.asList(nums).add
// List list = new ArrayList(Arrays.asList(nums));
// Integer.
// System.out.println();
}
}