Problems

Permutations

medium
medium
arrays
backtracking
recursion

Return every permutation of distinct nums in depth-first input-choice order.

Constraints

  • 1 <= nums.length <= 7
  • All values are unique

Follow-up

How can in-place swapping reduce per-branch allocations?

Examples

Example 1

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

Example 2

Input: nums = [0,1]
Output: [[0,1],[1,0]]

Example 3

Input: nums = [1]
Output: [[1]]
🔒 5 hidden

Running will execute all 8 cases, including 5 hidden ones.