- 문제 링크: https://leetcode.com/problems/subsets-ii/

- 난이도: Medium

 

2024.09.16 - [Online Judge/LeetCode] - [LeetCode][Python] 78. Subsets

위 문제와 동일한데, 각 조합이 중복된 원소를 포함할 수 있다

class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        sz = len(nums)
        res = set()
        nums.sort()

        def backtrack(cur: int, picked: List[int]):
            if cur == sz:
                res.add(tuple(picked))
                return
            
            backtrack(cur+1, picked)
            backtrack(cur+1, picked + [nums[cur]])
        
        backtrack(0, [])

        return [list(x) for x in res]
반응형