zoukankan      html  css  js  c++  java
  • Leetcode练习(Python):数组类:第90题:给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。

    题目:
    给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。  说明:解集不能包含重复的子集。
    思路:
    与第78题思路一样,用迭代法,对重复的数据进行判断再进行操作就可以。
    程序:
    class Solution:
        def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
            nums.sort()
            length = len(nums)
            if length <= 0:
                return [[]]
            if length == 1:
                return [[],nums]
            result = [[]]
            auxiliary = []
            for index1 in range(length):
                if index1 >= 1 and nums[index1 - 1] == nums[index1]:
                    auxiliary = [[nums[index1]] + index2 for index2 in auxiliary]
                else:
                    auxiliary = [[nums[index1]] + index2 for index2 in result]
                result +=auxiliary
            return result
  • 相关阅读:
    性能学习笔记2-20150129
    Go语言版黑白棋
    Go语言图形界面开发:Go版GTK
    Go入门教程
    本人录制的视频资源(C/C++、Go、Qt、Linux等)
    C++11新特性学习
    protobuf入门教程
    一步步学习Linux多任务编程
    Linux 网络编程系列教程
    一步一步学习GTK+
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12766785.html
Copyright © 2011-2022 走看看