Permutations II
Generate all unique permutations of an array containing duplicate numbers using duplicate neighbor pruning.
01The problem
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
| Input | Output | Why |
|---|---|---|
nums = [1, 1, 2] | [[1, 1, 2], [1, 2, 1], [2, 1, 1]] | 3 unique permutations (instead of 3! = 6) |
nums = [1, 2, 3] | [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] | Distinct numbers case |
Constraints: 1 <= nums.length <= 8, -10 <= nums[i] <= 10.
Loading the rest of the lesson…