`

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

 

Solution 1:

public List<List<Integer>> permuteUnique(int[] num) {
    List<List<Integer>> result = new ArrayList<>();
    Arrays.sort(num);
    permutate(result, num, 0);
    return result;
}

public void permutate(List<List<Integer>> result, int[] num, int pos) {
    if(pos == num.length) {
        List<Integer> list = new ArrayList<Integer>();
        for(int a:num) list.add(a);
        result.add(list);
        return;
    }
    for(int i=pos; i<num.length; i++) {
        if(hasDuplicate(num, pos, i)) continue;
        swap(num, i, pos);
        permutate(result, num, pos+1);
        swap(num, i, pos);
    }
}

private boolean hasDuplicate(int[] num, int start, int end) {
    for(int i=start; i<end; i++) {
        if(num[i] == num[end]) return true;
    }
    return false;
}

private void swap(int[] num, int i, int j) {
    int tmp = num[i];
    num[i] = num[j];
    num[j] = tmp;
}

 

Solution 2:

public List<List<Integer>> permuteUnique(int[] num) {
    List<List<Integer>> result = new ArrayList<>();
    result.add(new ArrayList<Integer>());
    Arrays.sort(num);
    for(int i=0; i<num.length; i++) {
        Set<List<Integer>> currentSet = new HashSet<List<Integer>>();
		for (List<Integer> l : result) {
			for (int j = 0; j < l.size() + 1; j++) {
				List<Integer> entry = new ArrayList<Integer>(l);
				entry.add(j, num[i]);
				currentSet.add(entry);
			}
		}
		result = new ArrayList<List<Integer>>(currentSet);
    }
    return result;
}

 

分享到:
评论

相关推荐

    C语言-leetcode题解之47-permutations-ii.c

    c语言入门 C语言_leetcode题解之47-permutations-ii.c

    js-leetcode题解之47-permutations-ii.js

    js js_leetcode题解之47-permutations-ii.js

    C语言-leetcode题解之46-permutations.c

    c语言入门 C语言_leetcode题解之46-permutations.c

    js-leetcode题解之46-permutations.js

    js js_leetcode题解之46-permutations.js

    leetcode1-300.docx

    LeetCode 算法题解析 从给定的文件信息中,我们可以看到,这是一个关于 LeetCode 算法题的知识点总结。下面我们将对标题、描述、标签和部分内容进行详细的解析和总结。 标题:leetcode1-300.docx 这个标题表明了...

    leetcode2-leetcode-30days:30天内写30个LeetCode

    leetcode 2 LeetCode - 30 Days 前言 相信所有写程式的人在面试前,总是在揣测在白板题会被问到什么问题,而我们最常听到的准备方式就是“刷”。上有数百个可能是面试官问你的题目,我把它...Permutations 目录 ref:

    LeetCode-Hot-100

    例如,"Longest Increasing Subsequence"使用动态规划求解最长递增子序列,而"Permutations"则通过回溯法寻找所有可能的排列组合。 3. **字符串处理**:在C++中,处理字符串需要熟悉`std::string`类和相关函数。...

    leetcode2-leetcode2:leetcode2

    - 使用`itertools`模块提供的工具函数,如`combinations()`, `permutations()`等,处理组合和排列问题。 - 应用动态规划、贪心算法、回溯法等算法思想,解决复杂问题。 4. **'leetcode2-master'项目**: - '...

    leetcode怎么销号-LeetCode-Solutions:我自己的LeetCode解决方案

    leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone Number Medium 回溯、暴力 0034...

    _leetcode-python.pdf

    - Permutations / Permutations II: 生成所有可能的排列组合,考虑重复元素的情况。 - Rotate Image: 给定一个二维矩阵,将它顺时针旋转90度。 - Group Anagrams: 组合所有字母异位词,即将字母顺序不同但字母组合...

    leetcode怎么计算空间复杂度是指-LeetCode-Solution:我的第一个LeetCode解决方案

    leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 already!) 我经常在递归的结束地方忘记return!!! 题型一:经典暴力递归:(里面涉及到重复不重复的...

    leetcode分类-LeetCode:LeetCode在线裁判C++

    II 其他变种 编程之美 Preorder Traversal Inorder Traver sal postorder 非递归 不用栈! 字符串常用操作 字符串 各种转换 Pow(x,n) 优化 Permutations (交换 取子集两种方式) Trie树 11 中序遍历 无堆栈 (前序 ...

    leetcode题库-little-algorithm:LeetCode题目参考代码与详细讲解,公众号《面向大象编程》文章整理

    leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号《面向大象编程》上发表面试算法、LeetCode 题解相关文章,至今收获不少好评。此仓库是公众号内容的补充,包括公众号文章涉及到的题目的参考代码,以及 ...

    leetcode530-Leetcode_golang:Leetcode问题的Golang实现

    在LeetCode中,这可能包括"全排列"(Permutations)问题,即找出所有可能的元素排列方式。 3. **二进制搜索**:二进制搜索是一种在有序数组中查找特定元素的算法。它通过不断地将搜索区间减半来提高效率。LeetCode...

    leetcode棋盘-Algorithms:通用算法实现

    permutations (normal and better), sum_3.py, unique_paths.py + unique_paths2.py, nQueens.py 很少包含外部库,所以下面的就可以了。 编译任何 C 程序: gcc -o X Xc 执行 python 脚本: chmod 755 X.py --&gt; ....

    leetcode 46. Permutations 迭代+递归 python3

    LeetCode第46题,题目要求根据给定的整数集合,返回所有可能的排列组合。这个问题属于组合数学与算法设计的范畴,主要考察的是全排列的生成。 【解题思路】 1. **递归算法**:递归方法是从剩余未使用的元素中选择...

    2sumleetcode-LeetCode:力码

    leetcode 轮廓 1_count_and_say.cpp - super_ugly_number.cpp - Detect_Pattern.cpp - degree_of_array.cpp - 键盘.cpp - 2Sum_Data_Structure_Design.cpp - shuffle_array.cpp - permutations.cpp - kth_missing_...

Global site tag (gtag.js) - Google Analytics