Skip to main content
Back to problems
Leetcode
Medium
Arrays
Backtracking
Combinatorics
Permutations

Generate every possible ordering of a list of distinct numbers.

Acceptance 100%
Problem Statement

Problem

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

A permutation is a complete reordering of all elements. Two permutations are considered different if they differ at any position.

Return the result as a list containing every valid permutation.

Input Format

  • An integer array nums containing distinct values.

Output Format

  • A list of arrays, where each inner array is one permutation of nums.
  • The order of permutations does not matter.

Constraints

  • 1 <= nums.length
  • All values in nums are distinct.
  • Every element of nums must appear exactly once in each permutation.

Hints

  • Build the permutation one position at a time.
  • Track which elements have already been used in the current partial arrangement.
  • When the current arrangement reaches full length, record it as one answer.

Input Format

  • Integer array nums with distinct elements.

Output Format

  • All permutations of nums as a list of integer arrays.

Constraints

  • Elements are distinct.
  • Each permutation must use every element exactly once.
  • Return all possible orderings.
Examples
Sample cases returned by the problem API.

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

There are 3! = 6 ways to order three distinct numbers.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.