dsa · medium
Permutations II
Return every unique permutation of nums. Values in nums may repeat, so two permutations that are the same sequence must appear only once. Order of the list of permutations does not matter. Order inside each permutation does matter: [1,2,1] is different from [1,1,2].
Arguments
nums— array that may contain duplicate integers
Example
nums = [1,1,2] has three distinct permutations: [1,1,2], [1,2,1], and [2,1,1]. Swapping the two ones does not create a new sequence.
nums = [1,2,3] has all six distinct orderings.
nums = [1] → [[1]].
Constraints
1 <= nums.length <= 8 -10 <= nums[i] <= 10
Examples
Example 1
Input: [1,1,2] Expected: [[1,1,2],[1,2,1],[2,1,1]]
Example 2
Input: [1,2,3] Expected: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 3
Input: [1] Expected: [[1]]