Practice · Backtracking · 1 of 4

All Orderings

You are given nums, a list of distinct integers. Return every permutation of it: every list that holds each number of nums exactly once, in some order.

The permutations may come in any order (the numbers inside each one, of course, may not).

Example 1

Input
nums = [2, 5, 8]
Output
[[2, 5, 8], [2, 8, 5], [5, 2, 8], [5, 8, 2], [8, 2, 5], [8, 5, 2]]
Three choices for the first place, two for the second and one for the last: 3! = 6 orderings.

Example 2

Input
nums = [-1, 4]
Output
[[-1, 4], [4, -1]]

Constraints

  • nums holds 1 to 6 numbers.
  • Every number is between −10 and 10, and no number appears twice.