题目原文:

Given two integer arrays of size n , design a subquadratic algorithm to determine whether one is a permutation of the other. That is, do they contain exactly the same entries but, possibly, in a different order.

本质上就是求两个数组排序后是否相等,鉴于本节课学的是选择、插入、希尔排序,就选个最不熟悉的希尔排序来实现吧

 import java.util.Arrays;

 public class PermutationArrays {
private int[] a;
private int[] b;
private int n;
PermutationArrays(int n, int[] a, int[] b){
this.a = a;
this.b = b;
this.n = n;
}
private boolean less(int v, int w){
return v < w;
}
private void exch(int[] a, int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
private void sortByShell(int n, int[] a){
int h = 1;
while(h < n/3){
h = 3*h + 1;
}
while(h>=1){
for(int i = h; i<n;i++){
for(int j = i; j>=h && less(a[j],a[j-h]);j=j-h){
exch(a,j,j-h);
}
}
h = h/3;
}
}
public boolean isPermutation(){
sortByShell(n,a);
System.out.println(Arrays.toString(a));
sortByShell(n,b);
System.out.println(Arrays.toString(b));
for(int i=0;i<n;i++){
if(a[i] != b[i])
return false;
}
return true;
}
public static void main(String[] args){
int[] a = {1,2,4,5,6,11,9,7,8,0};
int[] b = {0,9,8,7,6,5,4,3,2,1};
PermutationArrays pa = new PermutationArrays(10,a,b);
System.out.println(pa.isPermutation());
}
}

Coursera Algorithms week2 基础排序 练习测验: Permutation的更多相关文章

  1. Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法

    第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...

  2. Coursera Algorithms week2 基础排序 练习测验: Intersection of two sets

    题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subq ...

  3. Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks

    题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...

  4. Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space

    题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...

  5. Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST

    题目原文: Given a binary tree where each 

  6. Coursera Algorithms week4 基础标签表 练习测验:Java autoboxing and equals

    1. Java autoboxing and equals(). Consider two double values a and b and their corresponding Double v ...

  7. Coursera Algorithms week2 栈和队列 练习测验: Stack with max

    题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push a ...

  8. Coursera Algorithms week1 查并集 练习测验:3 Successor with delete

    题目原文: Given a set of n integers S = {0,1,…,N-1}and a sequence of requests of the following form: Rem ...

  9. Coursera Algorithms week1 查并集 练习测验:2 Union-find with specific canonical element

    题目原文: Add a method find() to the union-find data type so that find(i) returns the largest element in ...

随机推荐

  1. Sandbox 沙盒

    In computer security, a sandbox is a security mechanism for separating running programs, usually in ...

  2. Redis 之服务器集群配置

    常见的集群架构如图: redis操作过程中数据同步的函数调用关系: 集群搭建: 1.修改3个redis.config 文件的: 2.启动2个redis服务器 当杀掉redis主进程Master时,由于 ...

  3. python SQLALchemy连接数据库。

    一.ORM与SQLALchemy简介 ORM 全程object Relational Mapping,对象关系映射.简单的说,ORM将数据库中的表与面向对象中的类建立了一种对应关系.这样在操作数据库时 ...

  4. 16.2 【C# 5】调用者信息特性

    16.2.1 基本行为 .NET 4.5引入了三个新特性(attribute),即 CallerFilePathAttribute . CallerLineNumber- Attribute 和 Ca ...

  5. Django REST framework 自定义(认证、权限、访问频率)组件

    本篇随笔在 "Django REST framework 初识" 基础上扩展 一.认证组件 # models.py class Account(models.Model): &qu ...

  6. 第二节:web爬虫之lxml解析库

    lxml是python的一个解析库,支持HTML和XML的解析,支持XPath解析方式,而且解析效率非常高.

  7. [LeetCode] 887. Super Egg Drop 超级鸡蛋掉落

    You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is iden ...

  8. 【Codeforces 1106E】Lunar New Year and Red Envelopes

    [链接] 我是链接,点我呀:) [题意] 给你k个红包,每个红包可以在si..ti的时间范围内拿走. 抢完红包之后你得到wi元,然后你需要在di+1时刻才能继续抢红包 时间是线性的从1..n 然后某个 ...

  9. 【郑轻邀请赛 D】hipercijevi

    [题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2130 [题意] [题解] 把那个管泛化成一个点; 然后把每一个在管里面的点都和它相连 ...

  10. Redis学习总结(1)——Redis内存数据库详细教程

    1.redis是什么 2.redis的作者何许人也 3.谁在使用redis 4.学会安装redis 5.学会启动redis 6.使用redis客户端 7.redis数据结构 – 简介 8.redis数 ...