3
3
import java .util .ArrayList ;
4
4
import java .util .List ;
5
5
6
- /**Given a collection of distinct numbers, return all possible permutations.
6
+ /**
7
+ * 46. Permutations
8
+ *
9
+ * Given a collection of distinct numbers, return all possible permutations.
7
10
8
11
For example,
9
12
[1,2,3] have the followi
10000
ng permutations:
14
17
[2,3,1],
15
18
[3,1,2],
16
19
[3,2,1]
17
- ]*/
20
+ ]
21
+
22
+ */
23
+
18
24
public class _46 {
19
- static class AcceptedSolution {
25
+
26
+ public static class Solution1 {
20
27
//this solution has a backtracking function that has a return type
21
- public static List <List <Integer >> permute (int [] nums ) {
28
+ public List <List <Integer >> permute (int [] nums ) {
22
29
List <List <Integer >> result = new ArrayList ();
23
- List <Integer > init = new ArrayList <>();
24
- result .add (init );
30
+ result .add (new ArrayList <>());
25
31
return backtracking (result , nums , 0 );
26
32
}
27
33
28
- private static List <List <Integer >> backtracking (List <List <Integer >> result , int [] nums , int pos ) {
34
+ private List <List <Integer >> backtracking (List <List <Integer >> result , int [] nums , int pos ) {
29
35
if (pos == nums .length ) {
30
36
return result ;
31
37
}
@@ -42,16 +48,15 @@ private static List<List<Integer>> backtracking(List<List<Integer>> result, int[
42
48
}
43
49
}
44
50
45
- static class AcceptedSolutionWithVoidType {
46
- public static List <List <Integer >> permute (int [] nums ) {
51
+ public static class Solution2 {
52
+ public List <List <Integer >> permute (int [] nums ) {
47
53
List <List <Integer >> result = new ArrayList ();
48
- List <Integer > init = new ArrayList <>();
49
- result .add (init );
54
+ result .add (new ArrayList <>());
50
55
recursive (result , nums , 0 );
51
56
return result ;
52
57
}
53
58
54
- private static void recursive (List <List <Integer >> result , int [] nums , int pos ) {
59
+ private void recursive (List <List <Integer >> result , int [] nums , int pos ) {
55
60
if (pos == nums .length ) {
56
61
return ;
57
62
}
@@ -72,9 +77,4 @@ private static void recursive(List<List<Integer>> result, int[] nums, int pos) {
72
77
}
73
78
}
74
79
75
- public static void main (String ... args ) {
76
- int [] nums = new int []{1 , 2 , 2 };
77
-
78
- }
79
-
80
80
}
0 commit comments