leetcode--90--子集 II

题目:
给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

输入: [1,2,2]
输出:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

题目链接:https://leetcode-cn.com/problems/subsets-ii

思路:
1、这题和78题有些类似https://www.jianshu.com/p/d5302c9afe45
,只是多个需要去重的部分

Python代码:

import copy

class Solution(object):
    def subsetsWithDup(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        if not nums:
            return [[]]

        ans = [[nums[0]],[]]
        for num in nums[1:]:
            ans_cp = copy.deepcopy(ans)
            for item_ls in ans_cp:
                item_ls.append(num)
                ans.append(item_ls)

            ans_new = []
            for item in ans:
                if sorted(item) not in ans_new:
                    ans_new.append(sorted(item))
            ans = ans_new

        return ans
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容