Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
53 lines (44 loc) · 1.4 KB

File metadata and controls

53 lines (44 loc) · 1.4 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

Difficulty: Medium

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

Example:

Input: [1,1,2]
Output:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

Solution

Language: Java

class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        Set<List<Integer>> listSet = new HashSet<>();
        if (nums.length == 0) {
            return lists;
        }
        if (nums.length == 1) {
            List<Integer> item = new ArrayList<>();
            item.add(nums[0]);
            lists.add(item);
            return lists;
        }
        List<List<Integer>> permute = this.permuteUnique(Arrays.copyOfRange(nums, 1, nums.length));
        for (List<Integer> integers : permute) {
            for (int i = 0; i <= integers.size(); i++) {
                ArrayList<Integer> integers1 = new ArrayList<>(integers);
                integers1.add(i, nums[0]);
                if (!listSet.contains(integers1)) {
                    lists.add(integers1);
                    listSet.add(integers1);
                }
            }
        }
        return lists;
    }
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.