Permutations
Given a nonempty list of distinct integers nums, return every permutation exactly once. Each row uses every input value once, in an order that matters. The…
Given a nonempty list of distinct integers nums, return every permutation exactly once. Each row uses every input value once, in an order that matters. The order of returned rows does not matter. Implement permute(nums), preserving the input's final values and order. Return the complete collection, not its count or a subset of arrangements.
- 1 <= nums.length <= 6 and -10 <= nums[i] <= 10. - Input values are distinct integers, not Booleans or fractional values. - Return exactly n! unique ordered integer rows, each of length n. - Outer result order is unrestricted; inner row order is essential. - Preserve the input's final values and order. - Coding retains all 720 results at n = 6. Complete teaching journeys support n <= 4 without truncating branches.
Example 1: input nums = [1,2,3]; output [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]. There are 3! = 6 arrangements; another outer row order is also correct.
Example 2: input nums = [0,1]; output [[0,1],[1,0]]. Zero is an ordinary value and swapping positions changes the arrangement.
Example 3: input nums = [0]; output [[0]]. One value has one complete arrangement, not an empty row.
Category: Recursion. Topic: recursion. Difficulty: Medium. Patterns: backtracking, recursion.