File tree Expand file tree Collapse file tree 1 file changed +8
-7
lines changed
scripts/algorithms/S/Subsets II Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 125 ms (Top 19.54%) | Memory: 44.7 MB (Top 48.96%)
1
2
var subsetsWithDup = function ( nums ) {
2
3
let result = [ ] ;
3
4
//sort the nums to avoid duplicates;
4
5
nums . sort ( ( a , b ) => a - b ) ;
5
6
result . push ( [ ] ) ;
6
-
7
+
7
8
let startIdx = 0 ;
8
9
let endIdx = 0 ;
9
-
10
+
10
11
for ( let i = 0 ; i < nums . length ; i ++ ) {
11
12
let current = nums [ i ] ;
12
13
startIdx = 0 ;
13
-
14
+
14
15
//check for duplicates and get the idx of last subset
15
16
if ( i > 0 && nums [ i ] === nums [ i - 1 ] ) {
16
17
startIdx = endIdx + 1 ;
17
18
}
18
19
endIdx = result . length - 1 ;
19
-
20
+
20
21
for ( let j = startIdx ; j < endIdx + 1 ; j ++ ) {
21
22
let set1 = result [ j ] . slice ( 0 ) ;
22
23
set1 . push ( current ) ;
23
24
result . push ( set1 ) ;
24
25
}
25
26
}
26
-
27
+
27
28
return result ;
28
-
29
- } ;
29
+
30
+ } ;
You can’t perform that action at this time.
0 commit comments