Given an array nums of distinct integers, return every ordering of nums, meaning every arrangement that uses each element exactly once. An array of length n has exactly n! of them.
To make the answer unique, return the orderings in ascending lexicographic order: compare two orderings element by element from the front, and the one with the smaller value at the first differing position comes first. Under this rule the ascending sort of nums is always first and the descending sort is always last.
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]]
Explanation: All 3! = 6 orderings, listed lexicographically. The three starting with 1 come first, then those starting with 2, then those starting with 3.
Example 2
Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3
Input: nums = [1]
Output: [[1]]
Explanation: A single element has exactly one ordering.
Constraints
1≤nums.length≤6
−10≤nums[i]≤10
All the integers of nums are unique.
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up