美文网首页
leetcode90. Subsets II

leetcode90. Subsets II

作者: 就是果味熊 | 来源:发表于2020-06-24 16:33 被阅读0次

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

    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],
    []
    ]

    class Solution:
        def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
            
            
            def backtrack(path,nums):
                if not nums:
                    return
                for i, num in enumerate(nums):
                    if i == 0 or nums[i] != nums[i-1]:
                        path.append(num)
                        res.append(path[:])
                        backtrack(path,nums[i+1:])
                        path.pop()
            nums.sort()
            path = []
            res = []
            backtrack(path,nums)
            return res + [[]]
    

    相关文章

      网友评论

          本文标题:leetcode90. Subsets II

          本文链接:https://www.haomeiwen.com/subject/wdarfktx.html