Back

Backtracking (Hard) / 46. Permutations

00:00
1/5

46. Permutations

Medium

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example:

Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
  • 1

    Backtracking: We need to explore all orderings.

  • 2

    State: Current permutation built so far.

  • 3

    Choice: For the next position, choose any number that hasn't been used yet.

  • 4

    Base Case: If the current permutation length equals len(nums), add to results.

PYTHON PLAYGROUND
PYTHON PLAYGROUND
⏳ Loading editor…