题目原文:

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. 六时出行 App 隐私政策

    六时出行 App 隐私政策   本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义 ...

  2. js 学习笔记---基本概念

    早已接触javascript多年之后,竟然还有这些概念混淆不清,毫不知情,说出来真实无地自容 ! 1.使用严格模式,"use strict",虽然不适用,但是要知道,以免别人使用时 ...

  3. =new、=null、.clear()、system.gc()的区别

    开发经验告诉我 = new是指向另一个地址空间 =null对象被回收 .clear()对象被清空,但是仍然指向原来的地址空间 这三种方式都并没有真正的清理内存 只有system.gc()是直接清理,但 ...

  4. Python变量的命名 单下划线和双下划线

    python命名变量的区别 foo: 一种约定,Python内部的名字,用来区别其他用户自定义的命名,以防冲突,就是例如__init__(),__del__(),__call__()这些特殊方法 _f ...

  5. JS布尔值(Boolean)转换规则

    原文作者: louis 原文链接: http://louiszhai.github.io/2015/12/11/js.boolean/ 语法 众所周知, JavaScript有五个基本的值类型:num ...

  6. 9.3.4 BeaufitulSoup4

    BeautifulSoup 是一个非常优秀的Python扩展库,可以用来从HTML或XML文件中提取我们感兴趣的数据,并且允许指定使用不同的解析器. 使用 pip install BeaufifulS ...

  7. 【codeforces 796C】Bank Hacking(用一些技巧来代替multiset)

    [题目链接]:http://codeforces.com/contest/796/problem/C [题意] 给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被 打 ...

  8. Maven学习总结(5)——聚合与继承

    Maven学习总结(五)--聚合与继承 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 <modules> <module> ...

  9. [BZOJ 4563]放棋子

    [BZOJ 4563]放棋子 题目 给你一个N*N的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列,要求你在这个矩阵上放N枚棋子(障碍的位置不能放棋子),要求你放N个棋子 ...

  10. RSAROLL

    题目:http://www.shiyanbar.com/ctf/1918 # -*- coding: utf-8 -*- import gmpy2 ciper = [704796792, 752211 ...