Permutations
MediumBacktracking
Given an array nums of distinct integers, return every permutation of it, that is, every possible ordering of all its values. Each permutation uses every value exactly once.
Return the permutations in any order, each exactly once.
Example 1
- Input:
- nums = [4,-2,7]
- Output:
- [[4,-2,7],[4,7,-2],[-2,4,7],[-2,7,4],[7,4,-2],[7,-2,4]]
- Explanation
- Three distinct values can be arranged in 3 x 2 x 1 = 6 orders: pick any of 3 for the first slot, any of the remaining 2 for the second, and the last value fills the third.
Example 2
- Input:
- nums = [-1,2]
- Output:
- [[-1,2],[2,-1]]
- Explanation
- Two values give two orderings.
Example 3
- Input:
- nums = [5]
- Output:
- [[5]]
- Explanation
- A single value has exactly one ordering.
Constraints
- 1 ≤
nums.length≤ 6 - -10 ≤
nums[i]≤ 10 - All
numsvalues are distinct