/46. Permutations

46. Permutations

Medium
Arrays81.7% acceptance

Given a list of unique integers input_list, return a list containing all possible permutations of input_list. 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 permutations of [4, 5, 6] are listed.

Example 2

Input: [7, 8]

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

Explanation: Both possible orderings of [7, 8].

Example 3

Input: [9]

Output: [[9]]

Explanation: Single element list has only one permutation.

Constraints

  • 1 <= len(input_list) <= 6
  • -10 <= input_list[i] <= 10 for all i
  • All elements in input_list 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]]