iq.lab
Python starts when a code cell comes near or you run one
mediumBacktrackingRecursion target 25 min

All orderings

You get a list nums of different integers. Return every permutation of it, as a list of lists. A permutation is one way to line up all the numbers in a row, using each number exactly once.

Return the permutations in any order. Inside a permutation the order is the whole point: [4, 5] and [5, 4] are two different permutations, and both must appear.

Example 1
Inputnums = [4, 5, 6]Output[[4, 5, 6], [4, 6, 5], [5, 4, 6], [5, 6, 4], [6, 4, 5], [6, 5, 4]]

3 choices for the first spot, then 2 for the second, then 1 for the last: 3 × 2 × 1 = 6 orderings.

Example 2
Inputnums = [8, -3]Output[[8, -3], [-3, 8]]

Two numbers can stand in two orders.

Example 3
Inputnums = [7]Output[[7]]

One number has one ordering.

Constraints
  • 1 ≤ len(nums) ≤ 8

  • -10 ≤ nums[i] ≤ 10

  • All numbers in nums are different.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.