题目原文:

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. 开源业务规则引擎JBoss Drools

    Drools 是什么? 规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.接受数据输入,解释业务规则,并根据业务规 ...

  2. windows ping 某个网段,不能运行指定的软件

    windows ping 某个网段,不能运行指定的软件 :begin @echo OFF color 0a Title Net Test Tool by:HRuinger Mode con cols= ...

  3. [Windows Server 2008] IIS自带FTP配置方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:IIS自带FT ...

  4. javascript的严格模式:use strict

    ECMAscript 5添加的运行模式,禁止一些非标准.不安全的操作. <script> "use strict"; console.log("这是严格模式. ...

  5. Redis 之消息发布与订阅(publish、subscribe)

    使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称 发布内容 一般做群聊,聊天室,发布公告信息等.

  6. Python之进程 基础知识 上

    阅读目录 理论知识 操作系统背景知识 什么是进程 进程调度 进程的并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 在python程序中的进程操作 multiprocess模块 进程的创建和mu ...

  7. APIshop精选接口助力双十一电商业务

    距离2018年双11的购物盛典已经不到一个月了,各大电商之间的战役已经悄然打响,今年的双11仍会是一场电商鏖战,想必又会打破2017年双11近2540亿的全网成交总额记录. 据统计,去年双11全天共产 ...

  8. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  9. 三、Scrapy中选择器用法

    官方示例源码<html> <head>  <base href='http://example.com/' />  <title>Example web ...

  10. Problem 29

    Problem 29 Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: 仔细看看以下a与b的组合 22=4, 2 ...