-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.py
42 lines (38 loc) · 1.08 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
19 / 19 test cases passed.
Runtime: 48 ms
Memory Usage: 15.1 MB
"""
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
ans, path = [], []
nums.sort()
self.dfs(0, nums, ans, path)
return ans
def dfs(self, start, nums, ans, path):
ans.append(path[:])
for i in range(start, len(nums)):
if i != start and nums[i] == nums[i - 1]:
continue
path.append(nums[i])
self.dfs(i + 1, nums, ans, path)
path.pop()
"""
19 / 19 test cases passed.
Runtime: 36 ms
Memory Usage: 14.9 MB
"""
class Solution2:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
ans, path = [], []
nums.sort()
def trackback(start, path):
ans.append(path[:])
for i in range(start, len(nums)):
if i != start and nums[i] == nums[i - 1]:
continue
path.append(nums[i])
trackback(i + 1, path)
path.pop()
trackback(0, path)
return ans