forked from gavinfish/leetcode-share
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path090 Subsets II.py
More file actions
39 lines (34 loc) · 970 Bytes
/
090 Subsets II.py
File metadata and controls
39 lines (34 loc) · 970 Bytes
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
'''
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Subscribe to see which companies asked this question
'''
class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = [[]]
nums.sort()
temp_size = 0
for i in range(len(nums)):
start = temp_size if i >= 1 and nums[i] == nums[i - 1] else 0
temp_size = len(result)
for j in range(start, temp_size):
result.append(result[j] + [nums[i]])
return result
if __name__ == "__main__":
assert Solution().subsetsWithDup([1, 2, 2]) == [[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]