47 LeetCode Java: Permutations II – Medium | cheonhyangzhang's programming
47 LeetCode Java: Permutations II – Medium
Problem:
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2]
have the following unique permutations:
[1,1,2]
, [1,2,1]
, and [2,1,1]
.
Thoughts:
This is similar to Permutations, the only difference is that the collection might contain duplicates.
So the modification is to avoid duplicate solution.
So the add condition is that for any duplicate elements, you only want to add it if the previous one ( duplicate) is added.
Say for 0 1 1, for the second 1, only insert it if the previous 1 is inserted so that we could avoid have two 0 1 1 permutation and 0 1 1 permutation.
Read full article from 47 LeetCode Java: Permutations II – Medium | cheonhyangzhang's programming