작심삼일

[LeetCode] 47 | Permutations II | Python 본문

스터디/코테

[LeetCode] 47 | Permutations II | Python

yun_s 2022. 5. 12. 09:49
728x90
반응형

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


문제

Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.


조건

  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10

내 풀이

itertools의 permutations를 사용한다.

다만 예시에서 주어진 것처럼 [1, 1, 2]와 같이 중복된 숫자가 있을 경우, permutation을 진행했을 때 중복된 집합이 나올 수 있다.

이는 set으로 제거한다.


코드

from itertools import permutations

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        return set(permutations(nums))
728x90
반응형
Comments