Skip to content

47. Permutations II

Edited by Charles

Medium · Python · Backtrack

Problem

Read the original problem on LeetCode.

The official page contains the problem statement, examples, and constraints.

Explanation, examples & other approaches

Core idea

Recall 46. Permutations for the core idea of the permutation. Recall 491. Non-decreasing Subsequences for the core idea of non-duplicate subsets.

Original study notes follow, including intermediate snippets and earlier attempts.

Note:

  1. Recall 46. Permutations for the core idea of the permutation.
  2. Recall 491. Non-decreasing Subsequences for the core idea of non-duplicate subsets.
Study record
Field Value
Attempts 1
Mastery ❚
The Latest Attempt January 25, 2026

Python solution

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        result = []
        self.backtrack(nums, [False] * len(nums), [], result)
        return result

    def backtrack(self, nums, used, path, result):
        if len(path) == len(nums):
            result.append(path[:])
            return

        uset = set()
        for i in range(len(nums)):
            if used[i] or nums[i] in uset:
                continue

            uset.add(nums[i])
            used[i] = True
            path.append(nums[i])
            self.backtrack(nums, used, path, result)
            path.pop()
            used[i] = False

Back to Day 22 Backtrack IV · All Algorithm notes