hard Array

Permutations

Write a function permute that takes an array of distinct integers and returns all possible permutations.

Function Signature

function permute(nums);

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: The permutations of [1,2,3] are [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]].

Example 2:

Input: nums = [0,1]
Output: [[0,1],[1,0]]
Explanation: The permutations of [0,1] are [[0,1],[1,0]].

Example 3:

Input: nums = [1]
Output: [[1]]
Explanation: The permutations of [1] are [[1]].

Solve this problem

Start your 7-day free trial to solve this problem in our code editor with instant feedback.

Start 7-Day Free Trial to Solve This Problem

Related Problems