FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/PermutationsII.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
PermutationsII.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (70 loc) · 2.47 KB
Breadcrumbs
coding-Interview
/
PermutationsII.java
Copy path
File metadata and controls
83 lines (70 loc) · 2.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
com
.
leetcode
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
/**
Example 1:
Input: nums = [1,1,2]
Output:
[[1,1,2],
[1,2,1],
[2,1,1]]
Example 2:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
**/
public
class
PermutationsII
{
class
Solution
{
public
List
<
List
<
Integer
>>
permuteUnique
(
int
[]
nums
) {
List
<
List
<
Integer
>>
result
=
new
ArrayList
<>();
List
<
Integer
>
list
=
new
ArrayList
<>();
//first list
for
(
int
n
:
nums
) {
list
.
add
(
n
);
}
backtrack
(
0
,
nums
,
list
,
result
);
return
result
;
}
public
void
backtrack
(
int
index
,
int
[]
nums
,
List
<
Integer
>
list
,
List
<
List
<
Integer
>>
result
) {
if
(
index
==
list
.
size
() && !
result
.
contains
(
list
)) {
// last tree branch issue
//the recursion start swaping from last element
result
.
add
(
new
ArrayList
<>(
list
));
//copy and add
}
for
(
int
i
=
index
;
i
<
nums
.
length
;
i
++) {
swap
(
list
,
index
,
i
);
//swap all other with this index
backtrack
(
index
+
1
,
nums
,
list
,
result
);
swap
(
list
,
index
,
i
);
//reset
}
}
private
void
swap
(
List
<
Integer
>
list
,
int
index1
,
int
index2
) {
int
temp
=
list
.
get
(
index1
);
list
.
set
(
index1
,
list
.
get
(
index2
));
list
.
set
(
index2
,
temp
);
}
}
//Alternate Optimized
class
Solution2
{
public
List
<
List
<
Integer
>>
permuteUnique
(
int
[]
nums
) {
List
<
List
<
Integer
>>
res
=
new
ArrayList
<>();
Arrays
.
sort
(
nums
);
boolean
[]
visited
=
new
boolean
[
nums
.
length
];
helper
(
nums
,
res
,
new
ArrayList
<>(),
visited
);
return
res
;
}
public
void
helper
(
int
[]
nums
,
List
<
List
<
Integer
>>
res
,
List
<
Integer
>
ans
,
boolean
[]
visited
) {
if
(
ans
.
size
() ==
nums
.
length
) {
res
.
add
(
new
ArrayList
<>(
ans
));
return
;
}
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++) {
if
(
visited
[
i
] ||
i
>
0
&&
nums
[
i
] ==
nums
[
i
-
1
] && !
visited
[
i
-
1
]) {
continue
;
}
ans
.
add
(
nums
[
i
]);
visited
[
i
] =
true
;
helper
(
nums
,
res
,
ans
,
visited
);
ans
.
remove
(
ans
.
size
() -
1
);
visited
[
i
] =
false
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL