Home
DSA Patterns
Backtracking
Permutations
Permutations (LeetCode 46)
Permutations (LeetCode 46): generate every permutation of distinct numbers with backtracking: choose an unused number, recurse, then un-choose it so the next branch starts clean.
Problem Understanding
Permutations: given an array nums of distinct integers, return all possible orderings of them, in any order.
Example: nums = [1, 2, 3] returns [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]], which is 3! = 6 permutations.
The Intuition: Fill Slots, Then Undo
A permutation is n slots filled one at a time. Slot 1 can be any of n numbers, slot 2 any of the remaining n − 1, and so on. That decision tree is exactly what backtracking explores:
- Choose: pick a number that is not used yet, mark it, append it.
- Explore: recurse to fill the next slot.
- Un-choose: pop it and clear the mark, so the next choice at this level starts from the same state.
When the path is full, record a copy. Compare Subsets, where order does not matter and each level only looks forward, and Combination Sum, which adds a target.
Permutations Visualization
EMPTY PATH
nums (used numbers tinted)
0
1
2
path
permutations (0)
None yet
Empty path, nothing used. Fill one slot at a time
Next
Slot 1: choose 1. Path [1]
nums
The numbers are on top, with used ones tinted; the path being built is below. Each step either chooses a number, records a full path, or backs out of a choice. Finished permutations collect beside the figure. Try Four numbers to watch all 24 appear.
Permutations Dry Run
Input: nums = [1, 2, 3]
| Path | Action |
|---|---|
| [1] | choose 1 |
| [1, 2] | choose 2 |
| [1, 2, 3] | choose 3, record |
| [1, 2] → [1] | undo 3, undo 2 |
| [1, 3] | choose 3 |
| [1, 3, 2] | choose 2, record |
| [] | undo back to the root |
| [2] … | repeat with 2 first, then 3 first |
Answer: 6 permutations.
Permutations LeetCode Solution
The choose / explore / un-choose template with a used array. In C the result has n! rows, so it is sized up front.
Permutations Code
1var permute = function(nums) {2 const res = [], path = [], used = new Array(nums.length).fill(false);3 const backtrack = () => {4 if (path.length === nums.length) {5 res.push([...path]);6 return;7 }8 for (let i = 0; i < nums.length; i++) {9 if (used[i]) continue;10 used[i] = true;11 path.push(nums[i]);12 backtrack();13 path.pop();14 used[i] = false;15 }16 };17 backtrack();18 return res;19};
Permutations in Python, Java, C++, TypeScript and C
Permutations in Python
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res, path, used = [], [], [False] * len(nums)
def backtrack():
if len(path) == len(nums):
res.append(path[:])
return
for i in range(len(nums)):
if used[i]:
continue
used[i] = True
path.append(nums[i])
backtrack()
path.pop()
used[i] = False
backtrack()
return resPermutations in Java
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtrack(nums, new ArrayList<>(), new boolean[nums.length], res);
return res;
}
private void backtrack(int[] nums, List<Integer> path, boolean[] used, List<List<Integer>> res) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i]) continue;
used[i] = true;
path.add(nums[i]);
backtrack(nums, path, used, res);
path.remove(path.size() - 1);
used[i] = false;
}
}
}Permutations in C++
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
vector<int> path;
vector<bool> used(nums.size(), false);
backtrack(nums, path, used, res);
return res;
}
private:
void backtrack(vector<int>& nums, vector<int>& path, vector<bool>& used, vector<vector<int>>& res) {
if (path.size() == nums.size()) {
res.push_back(path);
return;
}
for (int i = 0; i < (int)nums.size(); i++) {
if (used[i]) continue;
used[i] = true;
path.push_back(nums[i]);
backtrack(nums, path, used, res);
path.pop_back();
used[i] = false;
}
}
};Permutations in TypeScript
function permute(nums: number[]): number[][] {
const res: number[][] = [], path: number[] = [], used: boolean[] = new Array(nums.length).fill(false);
const backtrack = (): void => {
if (path.length === nums.length) {
res.push([...path]);
return;
}
for (let i = 0; i < nums.length; i++) {
if (used[i]) continue;
used[i] = true;
path.push(nums[i]);
backtrack();
path.pop();
used[i] = false;
}
};
backtrack();
return res;
}Permutations in C
void backtrack(int* nums, int n, int* path, int depth, bool* used, int** res, int* count) {
if (depth == n) {
res[*count] = malloc(n * sizeof(int));
memcpy(res[(*count)++], path, n * sizeof(int));
return;
}
for (int i = 0; i < n; i++) {
if (used[i]) continue;
used[i] = true;
path[depth] = nums[i];
backtrack(nums, n, path, depth + 1, used, res, count);
used[i] = false;
}
}
int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
int total = 1;
for (int i = 2; i <= numsSize; i++) total *= i;
int** res = malloc(total * sizeof(int*));
int path[6];
bool used[6] = {false};
*returnSize = 0;
backtrack(nums, numsSize, path, 0, used, res, returnSize);
*returnColumnSizes = malloc(total * sizeof(int));
for (int i = 0; i < total; i++) (*returnColumnSizes)[i] = numsSize;
return res;
}Edge Cases & Common Mistakes
- Appending the path itself:
res.append(path)stores a reference that later pops empty; append a copy. - Forgetting to un-choose: if
used[i]is not reset after the recursive call, later branches skip numbers they should try. - One number: the only permutation is
[nums[0]]. - Duplicates in the input: this template would repeat permutations; Permutations II sorts the input and skips a number equal to its unused left neighbour.
- Swap-based alternative: swapping
nums[start]with each later index avoids the used array but produces a different order.
Summary
Fill the permutation slot by slot. At each level, for every unused number: mark it, append it, recurse, then pop and unmark it. Record a copy whenever the path is full. O(n · n!) time and O(n) extra space.
More practice: DSA interview questions with answers · patterns cheat sheet
