Permutations

MediumArraySortingBacktracking

Description

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Examples

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:

For 3 distinct elements, there are 3! = 6 permutations. Each arrangement places a different element in the first position, then arranges the remaining two.

Input:nums = [0,1]
Output:[[0,1],[1,0]]
Explanation:

For 2 distinct elements, there are 2! = 2 permutations: [0,1] and [1,0].

Input:nums = [1]
Output:[[1]]
Explanation:

A single element has only 1! = 1 permutation - the element by itself.

Constraints

  • 1 ≤ nums.length ≤ 6
  • -10 ≤ nums[i] ≤ 10
  • All the integers of nums are unique.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!