Skip to content
BASA DSA

Permutations

MediumBacktracking

Return all permutations of the distinct integers in nums. Result order is unrestricted, but element order inside each permutation is significant.

Example 1

Input
nums = [1,2,3]
Output
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Explanation

Choosing each of three values once in every position gives six ordered permutations.

Example 2

Input
nums = [7]
Output
[[7]]

Explanation

A singleton has one ordering.

Example 3

Input
nums = [4,9]
Output
[[4,9],[9,4]]

Explanation

[4,9] and [9,4] are distinct permutations.

Constraints

  • 1 <= len(nums) <= 6
  • Values are distinct

Function contractpermuteArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…