File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ //////////////////////////////////////////////////////////////////////////////
2
+ // Backtracking
3
+ // Time: O(n*2^n)
4
+ // Space: O(2^n)
5
+ //////////////////////////////////////////////////////////////////////////////
6
+
7
+ /**
8
+ * @param {number[] } nums
9
+ * @return {number[][] }
10
+ */
11
+ function subsetsWithDup ( nums ) {
12
+
13
+ nums . sort ( ( a , b ) => a - b ) ;
14
+ const results = [ ] ;
15
+ const result = [ ] ;
16
+ getSubsets ( ) ;
17
+ return results ;
18
+
19
+ /**
20
+ * @param {number } start = `0`
21
+ * @return {void }
22
+ */
23
+ function getSubsets ( start = 0 ) {
24
+
25
+ results . push ( result . slice ( ) ) ;
26
+
27
+ for ( let i = start ; i < nums . length ; ++ i ) {
28
+ if ( i !== start && nums [ i ] === nums [ i - 1 ] ) {
29
+ continue ;
30
+ }
31
+ result . push ( nums [ i ] ) ;
32
+ getSubsets ( i + 1 ) ;
33
+ result . pop ( ) ;
34
+ }
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments