@@ -9,10 +9,10 @@ public class Permutations {
9
9
10
10
public static void main (String [] args ) {
11
11
Permutations obj = new Permutations ();
12
- obj .permuteWithCleanRecursive (new int []{1 , 2 , 3 });
12
+ obj .permuteWithIterate (new int []{1 , 2 , 3 });
13
13
}
14
14
15
- public List <List <Integer >> permuteWithCleanRecursive (int [] nums ) {
15
+ public List <List <Integer >> permuteWithIterate (int [] nums ) {
16
16
LinkedList <List <Integer >> result = new LinkedList <>();
17
17
result .add (new ArrayList <Integer >());
18
18
int size ;
@@ -56,4 +56,32 @@ private void addItemInAllPosition(int[] nums, int start, List<Integer> list, Lis
56
56
addItemInAllPosition (nums , start + 1 , newList , result );
57
57
}
58
58
}
59
+
60
+
61
+ public List <List <Integer >> permuteWithPickupOrNo (int [] nums ) {
62
+ List <List <Integer >> resultList = new ArrayList <List <Integer >>();
63
+ if (nums == null || nums .length == 0 ) {
64
+ return resultList ;
65
+ }
66
+
67
+ recursive (nums , new ArrayList <Integer >(), resultList );
68
+
69
+ return resultList ;
70
+ }
71
+
72
+ public void recursive (int [] nums , List <Integer > list , List <List <Integer >> resultList ) {
73
+ if (list .size () == nums .length ) {
74
+ resultList .add (new ArrayList <Integer >(list ));
75
+ return ;
76
+ }
77
+
78
+ for (int item : nums ) {
79
+ if (list .contains (item )) {
80
+ continue ;
81
+ }
82
+ list .add (item );
83
+ recursive (nums , list , resultList );
84
+ list .remove (list .size () - 1 );
85
+ }
86
+ }
59
87
}
0 commit comments