【LeetCode】90. Subsets II

àì夳堔傛蜴生んèń 2022-03-16 12:46 271阅读 0赞

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:

  1. Input: [1,2,2]
  2. Output:
  3. [
  4. [2],
  5. [1],
  6. [1,2,2],
  7. [2,2],
  8. [1,2],
  9. []
  10. ]

Solution

  1. class Solution(object):
  2. def subsetsWithDup(self, nums):
  3. """ :type nums: List[int] :rtype: List[List[int]] """
  4. result = [[]]
  5. for x in nums:
  6. result.extend([subset + [x] for subset in result])
  7. out = []
  8. for item in result:
  9. if len(item) == 0:
  10. out.append(item)
  11. elif sorted(item) not in out:
  12. out.append(sorted(item))
  13. return out

https://leetcode.com/submissions/detail/211046725/

Code

GitHub:https://github.com/roguesir/LeetCode-Algorithm

  • 更新时间:2019-02-27

发表评论

表情:
评论列表 (有 0 条评论,271人围观)

还没有评论,来说两句吧...

相关阅读