File tree 1 file changed +7
-6
lines changed
scripts/algorithms/P/Permutations II
1 file changed +7
-6
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 122 ms (Top 61.38%) | Memory: 44.5 MB (Top 89.57%)
1
2
var permuteUnique = function ( nums ) {
2
3
const answer = [ ]
3
4
@@ -6,7 +7,7 @@ var permuteUnique = function(nums) {
6
7
if ( pos >= array . length ) {
7
8
answer . push ( array )
8
9
}
9
-
10
+
10
11
const setObject = new Set ( )
11
12
12
13
for ( let index = pos ; index < array . length ; index ++ ) {
@@ -15,15 +16,15 @@ var permuteUnique = function(nums) {
15
16
}
16
17
setObject . add ( array [ index ] )
17
18
18
- // swap numbers
19
+ // swap numbers
19
20
let temp = array [ pos ]
20
21
array [ pos ] = array [ index ]
21
22
array [ index ] = temp
22
-
23
+
23
24
perm ( pos + 1 , [ ...array ] )
24
-
25
+
25
26
// undo swapping for next iteration
26
- temp = array [ index ]
27
+ temp = array [ index ]
27
28
array [ index ] = array [ pos ]
28
29
array [ pos ] = temp
29
30
}
@@ -32,4 +33,4 @@ var permuteUnique = function(nums) {
32
33
perm ( 0 , nums )
33
34
34
35
return answer
35
- } ;
36
+ } ;
You can’t perform that action at this time.
0 commit comments