You get an array nums of distinct integers. Return every ordering of all its numbers: each permutation uses every number of nums exactly once, and two permutations differ when the numbers come in a different order.
An array of n numbers has n! permutations, and each must appear once. You may list the permutations in any order, but the numbers inside each one must be in the order of that permutation.
Three choices for the first number, two for the second and one for the last make 3! = 6 orderings.
Two numbers can come in either order.
A single number has one ordering: itself.
Constraints
1 <= nums.length <= 6-10 <= nums[i] <= 10nums are all different.Build each permutation one position at a time: for the next position, try every number not used yet.
Write an ordering down one position at a time. The first position can hold any of the n numbers. Once it is filled, the second can hold any of the n - 1 left over, and so on until one number remains for the last spot. That is where n! comes from, and it is also a recipe for listing them: fill the next position with each unused number in turn, and for each choice, fill the rest the same way.
Time O(n · n!)Space O(n) besides the answer
The full walkthrough and code are part of Premium