Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get an array nums of distinct integers, at most 6 of them. Return every permutation: every way to arrange all the numbers in a row. Here order matters, so [1, 2, 3] and [3, 2, 1] are different answers. Return the permutations in any order.
Example: nums = [1, 2, 3] → [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]. That's 3! = 3 × 2 × 1 = 6 arrangements.
The limit of 6 means at most 6! = 720 answers. The problem is asking you to list them all cleanly.
Start with brute force
A naive way: generate every sequence of length n where each position can be any of the n numbers (that's nⁿ sequences), and keep only the ones with no repeated number. For n = 6 that's 46,656 sequences to get 720 answers. About 98% of the work is thrown away.
The waste comes from choosing a number that's already in the sequence and only noticing at the end. If you refuse to choose a used number in the first place, every branch you walk leads to a real answer.
Building the intuition
How would you write out the permutations of [1, 2, 3] by hand? Probably like this:
- Put 1 first. Then the rest,
[2, 3], in every order:1 2 3,1 3 2. - Put 2 first. Then
[1, 3]in every order:2 1 3,2 3 1. - Put 3 first. Then
[1, 2]:3 1 2,3 2 1.
At each position you ask, "which numbers haven't I placed yet?", and try each of them in turn. As a tree:
[]
/ | \
[1] [2] [3]
/ \ / \ / \
[1,2] [1,3] [2,1] [2,3] [3,1] [3,2]
| | | | | |
[1,2,3] [1,3,2] [2,1,3] [2,3,1] [3,1,2] [3,2,1]Compare it with the Subsets tree. Two differences:
- Only the leaves are answers. A permutation must use every number, so you save only when the path has length
n. - The loop restarts from 0 at every level. In Subsets, after picking 2 you never looked at 1 again, because
{2, 1}was the same set as{1, 2}. Here[2, 1, 3]is a new answer, so after picking 2 you must be able to pick 1.
So you need a fast way to ask "is nums[i] already in the path?". Scanning the path costs O(n). A boolean array used, where used[i] is true while nums[i] sits in the path, answers it in O(1).
The optimal approach
- Keep
path,result, andused = [False] * n. - Write
backtrack():- If
len(path) == n, save a copy ofpathand return. - For each
ifrom0ton − 1:- If
used[i], skip it. - Choose: set
used[i] = True, pushnums[i]. - Explore: call
backtrack(). - Un-choose: pop
nums[i], setused[i] = False.
- If
- If
- Call
backtrack()and returnresult.
The subtle detail: un-choose means undo everything you did when you chose. Here choosing changed two things, the path and the used array. If you pop from path but forget used[i] = False, then after the first branch, number 1 stays "used" forever. You'd only get the permutations starting with 1. The rule for every backtracking problem: the state after un-choose must be exactly the state before choose.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
result = []
path = []
used = [False] * len(nums) # used[i]: is nums[i] already in path?
def backtrack() -> None:
if len(path) == len(nums):
result.append(path[:]) # a full arrangement; copy it
return
for i in range(len(nums)): # any unused number may go next, so start at 0
if used[i]:
continue
used[i] = True # choose
path.append(nums[i])
backtrack() # explore
path.pop() # un-choose: undo BOTH changes
used[i] = False
backtrack()
return resultclass Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> result;
vector<int> path;
vector<bool> used(nums.size(), false); // used[i]: is nums[i] already in path?
backtrack(nums, used, path, result);
return result;
}
private:
void backtrack(const vector<int>& nums, vector<bool>& used, vector<int>& path,
vector<vector<int>>& result) {
if (path.size() == nums.size()) {
result.push_back(path); // a full arrangement; push_back copies it
return;
}
for (int i = 0; i < (int)nums.size(); i++) { // any unused number may go next
if (used[i]) continue;
used[i] = true; // choose
path.push_back(nums[i]);
backtrack(nums, used, path, result); // explore
path.pop_back(); // un-choose: undo BOTH changes
used[i] = false;
}
}
};class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
boolean[] used = new boolean[nums.length]; // used[i]: is nums[i] already in path?
backtrack(nums, used, new ArrayList<>(), result);
return result;
}
private void backtrack(int[] nums, boolean[] used, List<Integer> path, List<List<Integer>> result) {
if (path.size() == nums.length) {
result.add(new ArrayList<>(path)); // a full arrangement; copy it
return;
}
for (int i = 0; i < nums.length; i++) { // any unused number may go next, so start at 0
if (used[i]) continue;
used[i] = true; // choose
path.add(nums[i]);
backtrack(nums, used, path, result); // explore
path.remove(path.size() - 1); // un-choose: undo BOTH changes
used[i] = false;
}
}
}/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function (nums) {
const result = [];
const path = [];
const used = new Array(nums.length).fill(false); // used[i]: is nums[i] already in path?
const backtrack = () => {
if (path.length === nums.length) {
result.push([...path]); // a full arrangement; copy it
return;
}
for (let i = 0; i < nums.length; i++) { // any unused number may go next, so start at 0
if (used[i]) continue;
used[i] = true; // choose
path.push(nums[i]);
backtrack(); // explore
path.pop(); // un-choose: undo BOTH changes
used[i] = false;
}
};
backtrack();
return result;
};Dry run
nums = [1, 2, 3]. used is written as three flags for positions 0, 1, 2.
| Step | path | used | What happens |
|---|---|---|---|
| 1 | [] | F F F | i = 0: choose 1 |
| 2 | [1] | T F F | i = 0 used, skip; i = 1: choose 2 |
| 3 | [1, 2] | T T F | i = 2: choose 3 |
| 4 | [1, 2, 3] | T T T | length 3: save |
| 5 | [1, 2] | T T F | un-choose 3; loop ends; un-choose 2 |
| 6 | [1] | T F F | i = 2: choose 3 |
| 7 | [1, 3] | T F T | i = 1: choose 2 → [1, 3, 2] save |
| 8 | [1] → [] | F F F | un-choose 2, 3, then 1 |
| 9 | [] | F F F | i = 1: choose 2 → [2], then 1 → 3 gives [2, 1, 3] |
| 10 | … | … | the same pattern gives [2, 3, 1], [3, 1, 2], [3, 2, 1] |
After step 8 everything is back to F F F, which is exactly why the 2-first branch can use 1 again.
Complexity
- Time: O(n · n!). There are
n!leaves. Copying each one costs O(n). The inner levels of the tree add up to fewer nodes than the leaves times a small factor, and each node's loop checksnslots, so the total stays within O(n · n!). - Space: O(n) extra for the recursion depth,
pathandused. The output itself takes O(n · n!).
Edge cases and traps
- Forgetting
used[i] = False. The classic bug. You'd only see permutations that begin with the first number. - Checking
nums[i] in pathinstead ofused[i]. It's correct for distinct numbers but costs O(n) per check. It also breaks as soon as values repeat. - Starting the loop at a
startindex. That's the Subsets loop. You'd only get[1, 2, 3]. - Saving
pathitself. Save a copy, or every answer ends up empty. - One element.
[1]returns[[1]]. The base case handles it.
Say it in the interview
"Order matters, so this is a different tree from Subsets. At each level any number not yet placed can go next, and I only save at the leaves, when the path has all n numbers. I keep a boolean used array so 'is this taken?' is O(1). I mark it and append it, recurse, then pop and unmark, so the state is clean for the next branch. There are n! leaves and each copy costs n, so it's O(n · n!) time and O(n) extra space."
Follow-ups interviewers ask
- "Can you do it without the used array?" Yes, by swapping. At depth
d, swap eachnums[i](fori ≥ d) into positiond, recurse ond + 1, then swap back. The prefixnums[0..d−1]is the path, and the suffix is the set of unused numbers. - "What if
numshas duplicates?" (That's Permutations II.) Sort, and skipnums[i]whennums[i] == nums[i − 1]andused[i − 1]is false. That means an equal copy was already tried at this position. - "Only the next permutation, not all of them?" Use LeetCode's Next Permutation method: find the rightmost dip, swap it with the next larger value to its right, and reverse the suffix. That's O(n) with no recursion.
Practise the same idea
This is Day 71 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free