-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
46 lines (40 loc) · 1 KB
/
Permutations.java
File metadata and controls
46 lines (40 loc) · 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
package questions100;
import java.util.*;
public class Permutations {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
//from n power of n to n!
ArrayList<ArrayList<Integer>> last = new ArrayList<ArrayList<Integer>>();
if(num.length ==0 || num == null)
return last;
if(last.isEmpty())
{
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(num[0]);
last.add(arr);
}
for (int i = 1; i < num.length; i++) {
int digit = num[i];
ArrayList<ArrayList<Integer>> next = new ArrayList<ArrayList<Integer>>();
for (ArrayList<Integer> al : last) {
for(int j = 0; j <=al.size();j++)
{
ArrayList<Integer> arr = new ArrayList<Integer>();
for(int m = 0; m <= al.size(); m++)
{
if(j == m)
arr.add(digit);
if(m < al.size())
{
arr.add(al.get(m));
}
}
next.add(arr);
}
}
last = next;
}
return last;
}
}