File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 63
63
78|[ Subsets] ( ./0078-subsets.js ) |Medium|
64
64
83|[ Remove Duplicates from Sorted List] ( ./0083-remove-duplicates-from-sorted-list.js ) |Easy|
65
65
88|[ Merge Sorted Array] ( ./0088-merge-sorted-array.js ) |Easy|
66
+ 90|[ Subsets II] ( ./0090-subsets-ii.js ) |Medium|
66
67
94|[ Binary Tree Inorder Traversal] ( ./0094-binary-tree-inorder-traversal.js ) |Easy|
67
68
98|[ Validate Binary Search Tree] ( ./0098-validate-binary-search-tree.js ) |Medium|
68
69
100|[ Same Tree] ( ./0100-same-tree.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 90. Subsets II
3
+ * https://leetcode.com/problems/subsets-ii/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
7
+ *
8
+ * The solution set must not contain duplicate subsets. Return the solution in any order.
9
+ */
10
+
11
+ /**
12
+ * @param {number[] } nums
13
+ * @return {number[][] }
14
+ */
15
+ var subsetsWithDup = function ( nums ) {
16
+ const seen = new Set ( ) ;
17
+ const result = [ ] ;
18
+ dfs ( [ ] , 0 ) ;
19
+
20
+ function dfs ( subset , start ) {
21
+ const key = subset . sort ( ( a , b ) => a - b ) . join ( '' ) ;
22
+ if ( ! seen . has ( key ) ) {
23
+ result . push ( subset ) ;
24
+ seen . add ( key ) ;
25
+ }
26
+ for ( let index = start ; index < nums . length ; index ++ ) {
27
+ dfs ( [ ...subset , nums [ index ] ] , index + 1 ) ;
28
+ }
29
+ }
30
+
31
+ return result ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments