-
Notifications
You must be signed in to change notification settings - Fork 0
/
046_Permutations46.java
87 lines (77 loc) · 2.7 KB
/
046_Permutations46.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> current = new ArrayList<>();
helperFunction(result, current, nums, 0);
return result;
}
public void helperFunction(List<List<Integer>> result, List<Integer> current, int [] nums, int start) {
if (current.size() == nums.length) {
result.add(new ArrayList<>(current));
return;
}
for (int i=start; i<nums.length; i++) {
if (current.contains(nums[i])) continue;
current.add(nums[i]);
helperFunction(result, current, nums, 0);
current.remove(current.size()-1);
}
}
}
*****************************************************************
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
permutations(nums,0,list);
return list;
}
public void permutations(int[] nums,int i,List<List<Integer>> list) {
if(i==nums.length) {
List<Integer> sublist = new ArrayList<>();
for(int j=0;j<nums.length;j++){
sublist.add(nums[j]);
}
list.add(sublist);
return;
}
for(int start=i;start<nums.length;start++) {
swap(nums,i,start);
permutations(nums,i+1,list);
swap(nums,i,start);
}
}
private void swap(int[] nums,int i,int j) {
int temp = nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
***************************************************************************
class Solution {
public List<List<Integer>> permute(int[] nums) {
// traverse through all nums and for each number, they can be in any position
List<Integer[]> res = new ArrayList<>();
res.add(new Integer[nums.length]);
for (int i = 0; i < nums.length; i++) {
int curNum = nums[i];
List<Integer[]> newRes = new ArrayList<>();
for (int j = 0; j < res.size(); j++) {
Integer[] curList = res.get(j);
for (int k = 0; k < curList.length; k++) {
if (curList[k] != null) {
continue;
}
Integer[] copy = curList.clone();
copy[k] = curNum;
newRes.add(copy);
}
}
res = newRes;
}
List<List<Integer>> ret = new ArrayList<>();
for (int i = 0; i < res.size(); i++) {
ret.add(Arrays.asList(res.get(i)));
}
return ret;
}
}