Permutations
Medium · backtracking
Given a list nums of distinct integers, return every possible permutation: every way to arrange all the numbers in a row, each number used exactly once.
The permutations may be returned in any order. Each permutation is a full-length list, and the result must contain no duplicates.
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]]
Three numbers give 3! = 6 arrangements. Their order in the answer does not matter.
Example 2
Input: nums = [0, 1]
Output: [[0, 1], [1, 0]]
Here the order inside each list matters: [0, 1] and [1, 0] are different permutations.
Example 3
Input: nums = [7]
Output: [[7]]
Constraints
- 1 <= len(nums) <= 6
- -10 <= nums[i] <= 10
- All the numbers in nums are distinct.