Permutations
Given an array `nums` of distinct integers, return all the possible permutations. You can return the answer in any order. **Examples:** ```typescript permute([1, 2, 3]) // [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] permute([0, 1]) // [[0,1],[1,0]] permute([1]) // [[1]] ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(n * n!)
- Space complexity: O(n)
There are n! permutations, each taking O(n) time to construct.
Follow-Up Questions Interviewers Ask About Permutations
- How can you optimize the lookup if (!curr.includes(num))?
Use a boolean array used[i] or swap in-place within the nums array to achieve O(1) decision time per choice.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.