问题描述:给定一个数组,数组里面有重复元素,求全排列。

算法分析:和上一道题一样,只不过要去重。

  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.Set;
  5.  
  6. public class PermutationsUnique {
  7. public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
  8. ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
  9. permuteUnique(num, 0, result);
  10. return result;
  11. }
  12.  
  13. private void permuteUnique(int[] num, int start, ArrayList<ArrayList<Integer>> result) {
  14.  
  15. if (start >= num.length ) {
  16. ArrayList<Integer> item = convertArrayToList(num);
  17. result.add(item);
  18. }
  19.  
  20. for (int j = start; j < num.length; j++) {
  21. if (containsDuplicate(num, start, j)) {
  22. swap(num, start, j);
  23. permuteUnique(num, start + 1, result);
  24. swap(num, start, j);
  25. }
  26. }
  27. }
  28.  
  29. private ArrayList<Integer> convertArrayToList(int[] num) {
  30. ArrayList<Integer> item = new ArrayList<Integer>();
  31. for (int h = 0; h < num.length; h++) {
  32. item.add(num[h]);
  33. }
  34. return item;
  35. }
  36. //nums[start]和nums[end]交换,如果start-end之间有nums[i]==nums[end],那说明它以前交换过,就不用重复了。
  37. private boolean containsDuplicate(int[] arr, int start, int end) {
  38. for (int i = start; i < end; i++) {
  39. if (arr[i] == arr[end]) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45.  
  46. private void swap(int[] a, int i, int j) {
  47. int temp = a[i];
  48. a[i] = a[j];
  49. a[j] = temp;
  50. }
  51.  
  52. //这种方法和Permutation一样,因为用set了,所以就已经去重了。
  53. public static List<List<Integer>> permuteUnique2(int[] num) {
  54. List<List<Integer>> returnList = new ArrayList<>();
  55. returnList.add(new ArrayList<Integer>());
  56.  
  57. for (int i = 0; i < num.length; i++) {
  58. Set<ArrayList<Integer>> currentSet = new HashSet<>();
  59. for (List<Integer> l : returnList) {
  60. for (int j = 0; j < l.size() + 1; j++) {
  61. l.add(j, num[i]);
  62. ArrayList<Integer> T = new ArrayList<Integer>(l);
  63. l.remove(j);
  64. currentSet.add(T);
  65. }
  66. }
  67. returnList = new ArrayList<>(currentSet);
  68. }
  69.  
  70. return returnList;
  71. }
  72.  
  73. public static void main(String[] args)
  74. {
  75. Permutations pt = new Permutations();
  76. int[] num = {1,2,1,3};
  77. System.out.println(pt.permute(num).size());
  78. System.out.println(pt.permute(num));
  79. }
  80. }

PermutationsUnique,求全排列,去重的更多相关文章

  1. 求全排列Permutation

    是在教材(<计算机算法设计与分析(第4版)>王晓东 编著)上看见的关于求全排列的算法: 我们可以看一下书上怎么写的: #include<bits/stdc++.h> using ...

  2. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  3. 【康拓展开】及其在求全排列第k个数中的应用

    题目:给出n个互不相同的字符, 并给定它们的相对大小顺序,这样n个字符的所有排列也会有一个顺序. 现在任给一个排列,求出在它后面的第i个排列.这是一个典型的康拓展开应用,首先我们先阐述一下什么是康拓展 ...

  4. next_permutation()函数 和 prev_permutation() 按字典序求全排列

    next_permutation功能:    求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm> 与之完全相反的函数还有prev_permutation 这个 ...

  5. LeetCode:Permutations(求全排列)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  6. nyoj-366-D的小L(求全排列)

    D的小L 时间限制:4000 ms  |  内存限制:65535 KB 难度:2 描述       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给匡匡 ...

  7. [bzoj1072][SCOI2007][排列perm] (状态压缩+数位dp+排列去重)

    Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为2的有30种,末位为4的有60种. Input ...

  8. ACM 求全排列(字典序、邻位对换、递增进位制数,递减进位制数)

    字典序:(联合康托展开就也可以按照中介数求) 邻位对换.递增进位制数,递减进位制数:具体的实现和算法讲解如下: 代码..C++版的实现并不好..因为是挨个向后找的,如果K很大的时候会超时,不过...思 ...

  9. LeetCode46 回溯算法求全排列,这次是真全排列

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode的26篇文章,我们来实战一下全排列问题. 在之前的文章当中,我们讲过八皇后.回溯法,也提到了全排列,但是毕竟没有真正写 ...

随机推荐

  1. 170118、快速失败Vs安全失败(Java迭代器附示例)

    简介: 当错误发生时,如果系统立即关闭,即是快速失败,系统不会继续运行.运行中发生错误,它会立即停止操作,错误也会立即暴露.而安全失败系统在错误发生时不会停止运行.它们隐蔽错误,继续运行,而不会暴露错 ...

  2. MFC写入.ini文件的策略

    在使用WritePrivateProfileString的时候, 如果前两个参数已经在INI文件中存在,那该函数的作用就是修改键值,即楼主说的覆盖 如果第一个参数存在,第二个参数不存在,那作用是在IN ...

  3. CURLOPT_SSL_VERIFYPEER CURLOPT_SSL_VERIFYHOST

    w /** * Set curl options relating to SSL. Protected to allow overriding. * @param $ch curl handle */ ...

  4. Spring MVC 框架结构介绍(二)

    Spring MVC框架结构 Spring MVC是围绕DispatcherServlet设计的,DispatcherServlet向处理程序分发各种请求.处理程序默认基于@Controller和@R ...

  5. Diango思维图

    1,http 2,Django生命周期 3,Django部分命令 4,待续...

  6. eclipse导入项目,项目名出现红叉的情况(修改版)

    转至:http://blog.csdn.net/niu_hao/article/details/17440247 今天用eclipse导入同事发给我的一个项目之后,项目名称上面出现红叉,但是其他地方都 ...

  7. python中unicode和str的组合

    python中unicode对象和str对象拼接在一起,会自动将str对象转换成unicode对象 即:a="aa" b=u"bb" c=a+b type(c) ...

  8. Hadoop源码如何查看

    如何查看hadoop源码 1解压hadoop安装压缩文件成为文件夹,再进入解压后的文件夹下的src文件夹,选中core,hdfs,mapred三个文件夹

  9. Sqrt(x)

    这题没多大技巧性,只是牛顿迭代法多用于数值计算,这里出现有些意外.维基上有方法说明:http://zh.wikipedia.org/wiki/牛顿法 int sqrt(int x) { if (x = ...

  10. ServiceModel 元数据实用工具 (Svcutil.exe)

    ServiceModel 元数据实用工具用于依据元数据文档生成服务模型代码,以及依据服务模型代码生成元数据文档 一.SvcUtil.exe ServiceModel 元数据实用工具可在 Windows ...