LeetCode-47 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].

思路:递归地将每个元素与第一个元素进行交换。有一个额外的空间Set<Integer> duplicated,如果某个元素已经与第一个元素进行交换,则与这个元素相同的元素就不需要再与第一个元素交换。

代码如下:

List<List<Integer>> lists = new ArrayList<List<Integer>>();
    public List<List<Integer>> permuteUnique(int[] num) {
        permuteHelp(num, 0);
        return lists;
    }

    public void permuteHelp(int[] num, int start) {
        Set<Integer> duplicated = new HashSet<Integer>();
        if(start == num.length-1) {
            List<Integer> list = new ArrayList<Integer>();
            for(int i=0; i<num.length; i++) {
                list.add(num[i]);
            }
            lists.add(list);
        } else {
            for(int i=start; i<num.length; i++) {
                if(duplicated.contains(new Integer(num[i])))
                    continue;
                duplicated.add(new Integer(num[i]));
                swap(num, start, i);
                permuteHelp(num, start+1);
                swap(num, start, i);
            }
        }
    }

    public void swap(int[] num, int a, int b) {
        int tmp = num[a];
        num[a] = num[b];
        num[b] = tmp;
    }
时间: 2024-08-27 11:14:50

LeetCode-47 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

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 47 Permutations II(全排列)

题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使用递归算法: 传递参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列组合 nums保存初始的数组 used随着计

19.2.7 [LeetCode 47] Permutations II

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] ] 1 class Solution { 2 public: 3 vector<vector<int>> permuteUnique(vector<int&

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].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

[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 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

【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,当前排列数

[C++]LeetCode: 120 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的区别就是,输入的数字数组中包含重复的元素.如果我们对重复的元素不

[leetcode] 47. 全排列 II

47. 全排列 II 比上一个题多了个重复性 与46. 全排列完全一样的代码... class Solution { // 当没有下一个排列时return false public boolean nextPermutation(int[] nums) { if (nums.length == 1) { return false; } int p = -1; for (int i = nums.length - 2; i >= 0; i--) { if (nums[i] < nums[i +