Skip to contentKEEP THE CRAFTTHE SANCTUARY

Permutations of Distinct Values

Generate every ordering of a list of distinct integers.

The problem

Implement Solution.permute(nums). The integers in nums are all different. Return every permutation of nums, that is, every way to arrange all of its values in a row. For example, [1, 2] has the permutations [1, 2] and [2, 1]. The list of permutations can be in any order, but the order of values inside each permutation matters. Constraints: 1 to 6 distinct integers, each from -10 to 10.

Starter code

class Solution:
    def permute(self, nums):
        pass

Public examples

  1. nums = [1, 2, 3]gives[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
  2. nums = [-1, 0]gives[[-1, 0], [0, -1]]
  3. nums = [-10]gives[[-10]]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.