Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. Array

Permutation II

ID:47

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

Input: nums = [1,1,2]
Output:
[[1,1,2],
 [1,2,1],
 [2,1,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]]

Idea

Recursion + seen[] tracking

Construct recursion method "permute" with params: int[] nums, List<>curr, List<List<>> result, boolean[] seen

Iterate from 0 index to end of nums; If index already seen, continue; If index element equals to previous one and previous one is not seen, meaning permutation with previous element in the front is been created, then makes no difference to use current index element as the front one (1,1,2==1,1,2), so continue

Else, add current index element to curr, seen[index] = true, and recursively call on curr

Code

public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> curr = new ArrayList<>();
        boolean[] seen = new boolean[nums.length];
        permute(result, curr, seen, nums);
        return result;
    }
    
    private void permute(List<List<Integer>> result, List<Integer> curr, boolean[] seen, int[] nums){
        if(curr.size()==nums.length){
            result.add(new ArrayList(curr));
            return;
        }
        
        for(int i = 0; i<nums.length; i++){
            if(seen[i]){
                continue;
            }
            if(i > 0 && nums[i] == nums[i-1] && !seen[i-1]) continue;
            else{
                seen[i] = true;
                curr.add(nums[i]);
                permute(result, curr, seen, nums);
                seen[i] = false;
                curr.remove(curr.size()-1);
            }
        }
    }
PreviousValid SudokuNextCombination Sum

Last updated 3 years ago