Skip to content

Commit af3fc4c

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.8 MB (84.90%)
1 parent be54dbc commit af3fc4c

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<p>Given an integer array <code>nums</code> that may contain duplicates, return <em>all possible</em> <span data-keyword="subset"><em>subsets</em></span><em> (the power set)</em>.</p>
2+
3+
<p>The solution set <strong>must not</strong> contain duplicate subsets. Return the solution in <strong>any order</strong>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<pre><strong>Input:</strong> nums = [1,2,2]
8+
<strong>Output:</strong> [[],[1],[1,2],[1,2,2],[2],[2,2]]
9+
</pre><p><strong class="example">Example 2:</strong></p>
10+
<pre><strong>Input:</strong> nums = [0]
11+
<strong>Output:</strong> [[],[0]]
12+
</pre>
13+
<p>&nbsp;</p>
14+
<p><strong>Constraints:</strong></p>
15+
16+
<ul>
17+
<li><code>1 &lt;= nums.length &lt;= 10</code></li>
18+
<li><code>-10 &lt;= nums[i] &lt;= 10</code></li>
19+
</ul>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
3+
def dfs(i: int):
4+
if i == len(nums):
5+
result.append(temp[:])
6+
return
7+
temp.append(nums[i])
8+
dfs(i + 1)
9+
val = temp.pop()
10+
while i + 1 < len(nums) and nums[i + 1] == val:
11+
i += 1
12+
13+
dfs(i + 1)
14+
15+
nums.sort()
16+
result = []
17+
temp = []
18+
dfs(0)
19+
20+
return result

0 commit comments

Comments
 (0)