-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPermutation.java
52 lines (46 loc) · 1.17 KB
/
Permutation.java
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
/* daily coding problem #96 */
import java.util.*;
public class Permutation
{
public static void main(String[] args)
{
List<Integer> number = new ArrayList<>(Arrays.asList(1,2,3,4,5));
List<List<Integer>> permutations = new ArrayList<>();
List<Integer> tempList = new ArrayList<>();
findPermutation(permutations, tempList, number);
int i = 1;
for(List<Integer> list : permutations)
{
System.out.print(i + ") ");
for(int a : list)
{
System.out.print(a + " ");
}
System.out.println();
i++;
}
}
public static void findPermutation(List<List<Integer>> permutations, List<Integer> tempList, List<Integer> number)
{
// tempList started off as empty list
// when its size is the same as number size,
// it's one of the permutations thus adding into list
if(tempList.size() == number.size())
{
List<Integer> myList = new ArrayList<>(tempList);
permutations.add(myList);
}
else
{
for(int i = 0; i < number.size(); i++)
{
if(!tempList.contains(number.get(i)))
{
tempList.add(number.get(i));
findPermutation(permutations, tempList, number);
tempList.remove(tempList.size()-1);
}
}
}
}
}