【LeetCode】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/
Code
GitHub:https://github.com/roguesir/LeetCode-Algorithm
- 更新时间:2019-02-27
还没有评论,来说两句吧...