Permutations
Medium · Backtracking · Array · Backtracking
Problem
Given an array nums of distinct integers, return every possible ordering of them, in any order.
Examples
Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
3! = 6 orderings.
Input: nums = [1] Output: [[1]]
Constraints
- 1 <= len(nums) <= 6
- -10 <= nums[i] <= 10
- All values are distinct
Approaches
- In-place swapping — time O(n · n!), space O(n)
Related problems
Start free on Cariara · Coding problems · System design studio