Leetcode_med 90. 子集 II

描述

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

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

示例:

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

Python

backtracking

class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        res = []
        nums.sort()
        self.dfs(nums,0,[],res)
        return res
    
    def dfs(self,nums,index,path,res):
        res.append(path)
        for i in range(index,len(nums)):
            if i>index and nums[i]==nums[i-1]:
                continue
            self.dfs(nums,i+1,path+[nums[i]],res)
全部评论

相关推荐

顺利毕业的鸽子:这个不一定,找hr跟进一下
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务