前言
本系列博客爲平時刷LeetCode的記錄,每十道題一篇博客,持續更新,所有代碼詳見GitHub:https://github.com/roguesir/LeetCode-Algorithm
90. Subsets II
Introduce
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],
[]
]
Solution
class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = [[]]
for x in nums:
result.extend([subset + [x] for subset in result])
out = []
for item in result:
if len(item) == 0:
out.append(item)
elif sorted(item) not in out:
out.append(sorted(item))
return out
https://leetcode.com/submissions/detail/211046725/
- 更新時間:2019-02-27