DSA · Backtracking · #136
Permutations
Module 52 · difficulty 3/5·⏱ 30:00starts on first keystroke
Given an array `nums` of **distinct** integers, return all the possible permutations. You may return the answer in **any order**. Implement `permute(nums)` which returns an array of arrays, where each inner array is one distinct ordering of all elements of `nums`.
Examples
nums = [1,2,3]→[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]— All 3! = 6 orderings.nums = [0,1]→[[0,1],[1,0]]nums = [1]→[[1]]— A single element has exactly one permutation.
Constraints
- · 1 <= nums.length <= 6
- · -10 <= nums[i] <= 10
- · All integers of nums are distinct.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.