-
Notifications
You must be signed in to change notification settings - Fork 6
/
Permutation.java
64 lines (51 loc) · 1.71 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
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
/**
* Created by abc on 10/01/2016.
*/
public class Permutation {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
//starts with 1st index
return findPermutation(nums,0, result);
}
public List<List<Integer>> findPermutation(int[] nums, int startIndex , List<List<Integer>> result){
//if length of start increases the length of input , means we found
//all values , add it to list
if(startIndex >= nums.length){
result.add(convertToList(nums));
}
for (int i = startIndex; i < nums.length; i++) {
//swaps each index of item with all other index of items
//recursively
swap(nums,startIndex,i);
//find the next permutation from start+1 index till last item
findPermutation(nums,startIndex+1,result);
swap(nums,startIndex,i);
}
return result;
}
private List<Integer> convertToList(int[] array){
List<Integer> list = new ArrayList<>();
for (int i:
array) {
list.add(i);
}
return list;
}
private int[] swap(int[] a , int i, int j){
if(i != j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return a;
}
public static void main(String[] args) {
List<List<Integer>> result = new Permutation().permuteUnique(new int[]{1,2,3});
for (List<Integer> value:
result) {
System.out.println("-> "+value);
}
//System.out.println("Permutations "+ new Permutation().permuteUnique(new int[]{1,2,3}));
}
}