/46. Permutations

46. Permutations

Medium
Arrays81.7% acceptance

Given a list of distinct integers input_array, return a list of all possible permutations of input_array. The order of permutations in the output does not matter.

Example 1

Input: [4, 5, 6]

Output: [[4, 5, 6], [4, 6, 5], [5, 4, 6], [5, 6, 4], [6, 4, 5], [6, 5, 4]]

Explanation: All possible orderings of [4, 5, 6].

Example 2

Input: [7, 8]

Output: [[7, 8], [8, 7]]

Explanation: Two possible permutations for two distinct elements.

Example 3

Input: [9]

Output: [[9]]

Explanation: Only one permutation for a single element.

Constraints

  • 1 <= len(input_array) <= 6
  • -10 <= input_array[i] <= 10 for all i
  • All elements in input_array are unique
Python (current runtime)

Case 1

Input: [2, 3, 4]

Expected: [[2, 3, 4], [2, 4, 3], [3, 2, 4], [3, 4, 2], [4, 2, 3], [4, 3, 2]]

Case 2

Input: [5, 1]

Expected: [[5, 1], [1, 5]]

Case 3

Input: [0]

Expected: [[0]]