lintcode-medium-Permutations II

Given a list of numbers with duplicate number in it. Find all unique permutations.

Example

For numbers [1,2,2] the unique permutations are:

[
  [1,2,2],
  [2,1,2],
  [2,2,1]
]

Challenge

Using recursion to do it is acceptable. If you can do it without recursion, that would be great!

class Solution {
    /**
     * @param nums: A list of integers.
     * @return: A list of unique permutations.
     */
    public ArrayList<ArrayList<Integer>> permuteUnique(ArrayList<Integer> nums){
        // write your code here

        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

        if(nums == null || nums.size() == 0)
            return result;

        ArrayList<Integer> line = new ArrayList<Integer>();
        boolean[] visited = new boolean[nums.size()];

        Collections.sort(nums);

        helper(result, line, visited, nums);

        return result;
    }

    public void helper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> line, boolean[] visited, ArrayList<Integer> nums){

        if(line.size() == nums.size()){
            result.add(new ArrayList<Integer>(line));
            return;
        }

        for(int i = 0; i < nums.size(); i++){
            if(visited[i])
                continue;
            else if(i > 0 && nums.get(i) == nums.get(i - 1) && !visited[i - 1])
                continue;

            line.add(nums.get(i));
            visited[i] = true;
            helper(result, line, visited, nums);
            line.remove(line.size() - 1);
            visited[i] = false;

        }

        return;
    }

}
时间: 2024-11-04 09:35:39

lintcode-medium-Permutations II的相关文章

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

LeetCode46,47 Permutations, Permutations II

题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) For example,[1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] LeetCode47 II Given a collection of numb

[Lintcode]15. Permutations/[Leetcode]46. Permutations

15. Permutations/46. Permutations 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers, return all possible permutations. Example Example 1: Input: [1] Output: [ [1]] Example 2: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3]

LeetCode 047 Permutations II

题目要求:Permutations II 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]. 代码如下: class Solution { public: vector

Permutations II leetcode java

题目: 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]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

LeetCode: Permutations II [046]

[题目] 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]. [题意] 给定一个候选数集合,候选集中可能存在重复数,返回所有的排列 [思路] 思路和Permutat

【LeetCode】Permutations II

Permutations II 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]. 首先分析一下与Permutations有何差异. 记当前位置为start,当前排列数

leetCode 47.Permutations II (排列组合II) 解题思路和方法

Permutations II 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]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

[LeetCode][JavaScript]Permutations II

Permutations II 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]. https://leetcode.com/problems/permutations

【leetcode刷题笔记】Permutations II

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]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上