80. Permutations

Medium · Backtracking

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

A permutation is an arrangement of all elements where order matters. For an array of length n, there are n! permutations.

Examples

Example 1
Input: [1, 2, 3]
Output: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
Explanation: All 6 permutations of [1, 2, 3]. The output can be in any order.
Example 2
Input: [0, 1]
Output: [[0, 1], [1, 0]]
Explanation: Two permutations of [0, 1].

Constraints