Permutations
Given an array of distinct integers, return all possible permutations.
Output each permutation on a line, space-separated, sorted lexicographically.
Example
1,2,3
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
- The algorithm starts by selecting the first element from the input array, which can be either 1, 2, or 3, resulting in three initial permutations: 1, 2, 3.
- Then, for each initial permutation, the algorithm generates additional permutations by swapping the remaining elements, i.e., for the initial permutation 1, the algorithm swaps 2 and 3, resulting in 1 3 2.
- The algorithm continues this process, recursively generating all possible permutations of the input array, resulting in a total of permutations.
- Finally, the permutations are sorted lexicographically, resulting in the output: 1 2 3, 1 3 2, 2 1 3, 2 3 1, 3 1 2, 3 2 1.
Constraints
- 1 <= len(nums) <= 6
- -10 <= nums[i] <= 10
- All integers are unique