Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8329c51

Browse files
#90. Subsets II
1 parent bec93e5 commit 8329c51

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎Backtracking/subset_with_dup.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Given an integer array nums that may contain duplicates, return all possible
2+
# subsets
3+
# (the power set).
4+
5+
# The solution set must not contain duplicate subsets. Return the solution in any order.
6+
7+
8+
9+
# Example 1:
10+
11+
# Input: nums = [1,2,2]
12+
# Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
13+
# Example 2:
14+
15+
# Input: nums = [0]
16+
# Output: [[],[0]]
17+
18+
19+
# Constraints:
20+
21+
# 1 <= nums.length <= 10
22+
# -10 <= nums[i] <= 10
23+
24+
25+
from typing import List
26+
class Solution:
27+
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
28+
ans = []
29+
def dfs(s, path):
30+
ans.append(path)
31+
32+
for i in range(s, len(nums)):
33+
if i > s and nums[i] == nums[i-1]:
34+
continue
35+
dfs(i+1, path+[nums[i]])
36+
nums.sort()
37+
dfs(0, [])
38+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /