-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubSetsII.py
35 lines (34 loc) · 978 Bytes
/
subSetsII.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
# Given a collection of integers that might contain duplicates, nums, return all possible subsets(the power set).
# Note: The solution set must not contain duplicate subsets.
# Example:
# Input: [1, 2, 2]
# Output:
# [
# [2],
# [1],
# [1, 2, 2],
# [2, 2],
# [1, 2],
# []
# ]
class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if nums is None:
return
if len(nums) == 0:
return [[]]
self.result = []
self.search(sorted(nums), [], 0) # sorted
return self.result
def search(self, nums, subset, startIndex):
self.result.append(list(subset)) # list() cp
for i in xrange(startIndex, len(nums)):
if i > startIndex and nums[i] == nums[i - 1]: # filter
continue
subset.append(nums[i])
self.search(nums, subset, i + 1)
subset.pop()