Return all orderings (permutations) of the given distinct integers. At each position, choose the next unused element in index order, which produces the order shown below.
permutations(nums)
Input format: space-separated integers. Output: each permutation's values joined by
spaces, permutations separated by ;.
[1, 2] // → "1 2;2 1"
[1, 2, 3] // → "1 2 3;1 3 2;2 1 3;2 3 1;3 1 2;3 2 1"
[] // → "" (one empty permutation)
A permutation is a complete arrangement of all n elements. There are n! of them: n choices for the first slot, n-1 for the second, down to 1 for the last. Build one arrangement at a time, slot by slot, trying every not-yet-placed element at each slot.
The root is an empty arrangement. Each node has one child per unused element on the path from root to that node — choosing a child places that element in the next slot. A leaf is reached once every element is placed, corresponding to one complete permutation. Every root-to-leaf path is a distinct choice sequence, and every ordering corresponds to exactly one such path — walking the whole tree produces every permutation exactly once.
Maintain a current partial arrangement and which elements are used:
That undo step is what makes it backtracking rather than one-shot recursion — the same slot gets tried with every candidate, reusing one array and one used-list instead of allocating fresh state per branch.
Each slot's loop considers every unused element exactly once — nothing skipped. Undoing the used-flag after each branch makes rejected elements available again for other branches, essential since different permutations reuse elements in different slots. Every arrangement corresponds to a unique "pick the next unused element" sequence, and the recursion tries every one — exactly n! permutations, each produced once.
You could remove one element from a pool and prepend it to permutations of the rest — equally valid, but it copies/removes from arrays at every level. The used-array approach just flips boolean flags, making each branch cheap to enter and leave.
def permutations(nums):
result = []
used = [False] * len(nums)
cur = []
def backtrack():
if len(cur) == len(nums):
result.append(list(cur))
return
for i in range(len(nums)):
if used[i]:
continue
used[i] = True
cur.append(nums[i])
backtrack()
cur.pop()
used[i] = False
backtrack()
return result
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.