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%)
12var subsetsWithDup = function ( nums ) {
23 let result = [ ] ;
34 //sort the nums to avoid duplicates;
45 nums . sort ( ( a , b ) => a - b ) ;
56 result . push ( [ ] ) ;
6-
7+
78 let startIdx = 0 ;
89 let endIdx = 0 ;
9-
10+
1011 for ( let i = 0 ; i < nums . length ; i ++ ) {
1112 let current = nums [ i ] ;
1213 startIdx = 0 ;
13-
14+
1415 //check for duplicates and get the idx of last subset
1516 if ( i > 0 && nums [ i ] === nums [ i - 1 ] ) {
1617 startIdx = endIdx + 1 ;
1718 }
1819 endIdx = result . length - 1 ;
19-
20+
2021 for ( let j = startIdx ; j < endIdx + 1 ; j ++ ) {
2122 let set1 = result [ j ] . slice ( 0 ) ;
2223 set1 . push ( current ) ;
2324 result . push ( set1 ) ;
2425 }
2526 }
26-
27+
2728 return result ;
28-
29- } ;
29+
30+ } ;
You can’t perform that action at this time.
0 commit comments