Permutations

MediumBacktracking

Given an array nums of distinct integers, return every possible permutation: every way to arrange all of its numbers in a row.

An array of n distinct numbers has n! permutations, and each must appear exactly once. You may return them in any order (but the order of numbers inside each permutation is, of course, what makes it that permutation).

Constraints

  • 1 <= nums.length <= 6
  • -20 <= nums[i] <= 20
  • All values in nums are different.

Examples

Example 1

Input: nums = [4,5,6]
Output: [[6,5,4],[5,6,4],[5,4,6],[6,4,5],[4,6,5],[4,5,6]]

Three distinct numbers can be ordered in 3! = 6 ways. The orderings may be listed in any order.

Example 2

Input: nums = [0]
Output: [[0]]

A single number has one ordering.

Example 3

Input: nums = [-1,1]
Output: [[1,-1],[-1,1]]

Two numbers: either one can go first.

JavaScript you'll use: Recursion, the call stack and memoization Arrays: create, read, update Destructuring, spread and rest

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
function permute(nums) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.