Back to Practice
#0285
Permutations of Distinct Numbers
MediumDSA28 min20 XP
Problem
Given distinct numbers, return all permutations in lexicographic order.
Why This Matters
Permutations are the purest backtracking problem: choose an unused value, recurse, then undo.
Function Signature
def permutations(nums):
Examples
Example 1
Inputnums = [1,2,3]
Output[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
Every number gets a chance to appear in every position.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Three numbers
Input[1,2,3]
Expectedsix permutations
3! equals 6.
Two numbers
Input[2,1]
Expected[[1, 2], [2, 1]]
Input is sorted first for stable output.
Hidden Test Categories
empty listsingle numberlarger distinct list