-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path46.java
More file actions
145 lines (140 loc) · 4.73 KB
/
46.java
File metadata and controls
145 lines (140 loc) · 4.73 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
__________________________________________________________________________________________________
0ms
class Solution {
List<List<Integer>> result;
public List<List<Integer>> permute(int[] nums) {
result = new ArrayList<>();
permuteHelper(0, nums.length - 1, nums);
return result;
}
public void permuteHelper(int left, int right, int[] currentNums) {
if(left == right) {
List<Integer> perm = new ArrayList<Integer>();
for(int n : currentNums) {
perm.add(n);
}
result.add(perm);
}
for(int i = left; i <= right; i++) {
currentNums = swap(currentNums, left, i);
permuteHelper(left+1, right, currentNums);
currentNums = swap(currentNums, left, i);
}
}
public int[] swap(int[] currentNums, int i, int j) {
int temp = currentNums[i];
currentNums[i] = currentNums[j];
currentNums[j] = temp;
return currentNums;
}
}
__________________________________________________________________________________________________
1ms
class Solution {
List<List<Integer>> list = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
boolean[] vis;
public List<List<Integer>> permute(int[] nums) {
if (null == nums || 0 == nums.length) {
return list;
}
vis = new boolean[nums.length];
dfs(nums, 0);
return list;
}
public void dfs(int[] nums, int index) {
if (index == nums.length) {
list.add(new ArrayList<>(temp));
return ;
}
for (int i = 0; i < nums.length; i++) {
if (!vis[i]) {
vis[i] = true;
temp.add(nums[i]);
dfs(nums, index + 1);
vis[i] = false;
temp.remove(temp.size() - 1);
}
}
}
}
__________________________________________________________________________________________________
2ms
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums);
return res;
}
public void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums){
if(tempList.size() == nums.length){
list.add(new ArrayList<>(tempList));
}else{
for(int i = 0; i < nums.length; i++){
if(tempList.contains(nums[i])) continue;
tempList.add(nums[i]);
backtrack( list, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}
}
__________________________________________________________________________________________________
34872 kb
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if(nums == null || nums.length == 0){
return result;
}
Set<Integer> hash = new HashSet<>();
recursion(nums,result,hash,new ArrayList<Integer>());
return result;
}
private void recursion(int[] nums, List<List<Integer>> result, Set<Integer> hash, List<Integer> cur){
if(cur.size() == nums.length){
result.add(new ArrayList<>(cur));
return;
}
for(int i = 0; i < nums.length; i++){
if(!hash.contains(nums[i])){
cur.add(nums[i]);
hash.add(nums[i]);
recursion(nums,result,hash,cur);
cur.remove(cur.size()-1);
hash.remove(nums[i]);
}
else{
continue;
}
}
}
}
__________________________________________________________________________________________________
34876 kb
class Solution {
public void permute(int[] nums, Set<Integer> bitMap, List<Integer> temp, List<List<Integer>> list){
if(bitMap.size() == 0)
list.add(new ArrayList<>(temp));
else{
for(int num : nums){
if(bitMap.contains(num)){
bitMap.remove(num);
temp.add(num);
permute(nums, bitMap, temp, list);
temp.remove(temp.size()-1);
bitMap.add(num);
}
}
}
}
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Set<Integer> bitMap = new HashSet<>();
for(int num: nums)
bitMap.add(num);
permute(nums, bitMap, new ArrayList<>(), list);
return list;
}
}
__________________________________________________________________________________________________