Permutations

Medium#46permutations

You get a list of different numbers. Return every order they can stand in, each once, in any order. Our example: [4, 5, 6] has 3! = 6 orders, from [4, 5, 6] to [6, 5, 4].

input
nums: an array of different integers.
returns
Every permutation of nums: each ordering of all its numbers, once. The permutations in any order; the numbers of a permutation in that permutation's order.
constraints
  • 1 ≤ nums.length ≤ 6
  • −10 ≤ nums[i] ≤ 10
  • all the numbers of nums are different
our 10 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
function permute(nums) {

}
Ctrl ↵
format
reset

saved in this browser

10 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more backtracking problems

Pattern “Backtracking” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.