알고리즘/LeetCode
LeetCode 풀기 - 47. Permutations II
앤테바
2022. 3. 8. 07:27
반응형
47. Permutations II
https://leetcode.com/problems/permutations-ii/
Permutations II - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제)
솔루션1) 백트래킹
백트래킹 방식으로 순열을 한땀 한땀 구현합니다.
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
res = []
def recur(cur, remain):
if not remain:
res.append(tuple(cur))
return
for i, n in enumerate(remain):
recur(cur + [n], remain[:i] + remain[i+1:])
recur([], nums)
return set(res)
솔루션2) permutations()
파이썬 permutations() 함수를 사용하였습니다.
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
nums = tuple(permutations(nums, len(nums)))
unique_nums = dict.fromkeys(nums, None)
return list(unique_nums)
함께 보면 좋은 글:
2021.11.11 - [알고리즘/LeetCode] - LeetCode 풀기 - 46. Permutations
LeetCode 풀기 - 46. Permutations
46. Permutations https://leetcode.com/problems/permutations/ Permutations - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prep..
wellsw.tistory.com
반응형