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

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

 import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; public class PermutationsUnique {
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
permuteUnique(num, 0, result);
return result;
} private void permuteUnique(int[] num, int start, ArrayList<ArrayList<Integer>> result) { if (start >= num.length ) {
ArrayList<Integer> item = convertArrayToList(num);
result.add(item);
} for (int j = start; j < num.length; j++) {
if (containsDuplicate(num, start, j)) {
swap(num, start, j);
permuteUnique(num, start + 1, result);
swap(num, start, j);
}
}
} private ArrayList<Integer> convertArrayToList(int[] num) {
ArrayList<Integer> item = new ArrayList<Integer>();
for (int h = 0; h < num.length; h++) {
item.add(num[h]);
}
return item;
}
//nums[start]和nums[end]交换,如果start-end之间有nums[i]==nums[end],那说明它以前交换过,就不用重复了。
private boolean containsDuplicate(int[] arr, int start, int end) {
for (int i = start; i < end; i++) {
if (arr[i] == arr[end]) {
return false;
}
}
return true;
} private void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
} //这种方法和Permutation一样,因为用set了,所以就已经去重了。
public static List<List<Integer>> permuteUnique2(int[] num) {
List<List<Integer>> returnList = new ArrayList<>();
returnList.add(new ArrayList<Integer>()); for (int i = 0; i < num.length; i++) {
Set<ArrayList<Integer>> currentSet = new HashSet<>();
for (List<Integer> l : returnList) {
for (int j = 0; j < l.size() + 1; j++) {
l.add(j, num[i]);
ArrayList<Integer> T = new ArrayList<Integer>(l);
l.remove(j);
currentSet.add(T);
}
}
returnList = new ArrayList<>(currentSet);
} return returnList;
} public static void main(String[] args)
{
Permutations pt = new Permutations();
int[] num = {1,2,1,3};
System.out.println(pt.permute(num).size());
System.out.println(pt.permute(num));
}
}

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. python框架Scrapy中crawlSpider的使用

    一.创建Scrapy工程 #scrapy startproject 工程名 scrapy startproject demo3 二.进入工程目录,根据爬虫模板生成爬虫文件 #scrapy genspi ...

  2. mongoDB之find()

    一.find方法 db.collection_name.find();查询 查询所有结果 1) db.users.find();类似于select * from users; 指定返回那些列(键) 2 ...

  3. 巨蟒python全栈开发-第24天 内置常用模块3

    一. 1.re模块基础知识 2.python模块&re正则模块 3.实战:re模块&python(链家&电影天堂&豆瓣) 复习:上节课重点(1)sys.path 模块的 ...

  4. Let's encrypt申请泛域名证书

    1.下载工具 wget https://dl.eff.org/certbot-auto chmod a+x ./certbot-auto 2.初始化 ./certbot-auto 3.获取证书(1) ...

  5. Linux内核设计与实现——内核同步

    内核同步 同步介绍 同步的概念 临界区:也称为临界段,就是訪问和操作共享数据的代码段. 竞争条件: 2个或2个以上线程在临界区里同一时候运行的时候,就构成了竞争条件. 所谓同步.事实上防止在临界区中形 ...

  6. 正向代理、Nginx(反向代理、负载均衡、静态资源服务器)

    淘宝tengine文档(本质就是淘宝版的Nginx) http://tengine.taobao.org/book/index.html

  7. mysql 建立表之间关系 练习 2

    创建数据库db6 create database db6 charset=utf8; user db6; # 创建班级表 mysql) not null unique); Query OK, rows ...

  8. 词性标注算法之CLAWS算法和VOLSUNGA算法

    背景知识 词性标注:将句子中兼类词的词性根据上下文唯一地确定下来. 一.基于规则的词性标注方法 1.原理 利用事先制定好的规则对具有多个词性的词进行消歧,最后保留一个正确的词性. 2.步骤 ①对词性歧 ...

  9. Linux定时器 使用

    1.alarm alarm()执行后,进程将继续执行,在后期(alarm以后)的执行过程中将会在seconds秒后收到信号SIGALRM并执行其处理函数. #include <stdio.h&g ...

  10. Python之验证码

    Python生成随机验证码,需要使用PIL模块. 安装: ? 1 pip3 install pillow 基本使用 1. 创建图片 ? 1 2 3 4 5 6 7 8 9 from PIL impor ...