Coursera Algorithms week2 基础排序 练习测验: Permutation
题目原文:
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的更多相关文章
- Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...
- 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 ...
- Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks
题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...
- 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 ...
- Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST
题目原文: Given a binary tree where each
- Coursera Algorithms week4 基础标签表 练习测验:Java autoboxing and equals
1. Java autoboxing and equals(). Consider two double values a and b and their corresponding Double v ...
- Coursera Algorithms week2 栈和队列 练习测验: Stack with max
题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push a ...
- 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 ...
- 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 ...
随机推荐
- mvc 类中对应数据库属性
[StringLength()] //可空 对应数据库可空 [DefaultValue("")] [DisplayName("添加人用户名")] public ...
- Memcached 在Linux上的安装
1.安装libevent wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libeve ...
- Centos7自动式脚本搭建jumpserver
JumpServer脚本 这里需要安装阿里的yum源和epel源并解压: epel源地址https://mirrors.tuna.tsinghua.edu.cn/epel// 安装阿里互联网yum仓库 ...
- 图表实现基于SVG或Canvas
Highcharts 基于SVG,方便自己定制,但图表类型有限. Echarts 基于Canvas,适用于数据量比较大的情况. D3.v3 基于SVG,方便自己定制:D3.v4支持Canvas+SVG ...
- Linux之iptables(四、网络防火墙及NAT)
网络防火墙 iptables/netfilter网络防火墙: (1) 充当网关 (2) 使用filter表的FORWARD链 注意的问题: (1) 请求-响应报文均会经由FORWARD链,要注意规则的 ...
- Django——13 Auth系统 登陆注册实例 权限的实现
Django Auth系统中的表 注册登陆实例 权限的实现 登陆权限 操作权限 组操作 Auth系统中的表 从表的名称我们就能看出,auth_user,auth_group,auth_permiss ...
- 协程,greenlet原生协程库, gevent库
协程简介 协程(coroutine),又称为微线程,纤程,是一种用户级的轻量级线程.协程拥有自己的寄存器上下文和栈. 协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来时,恢复之前保存的上下文 ...
- 洛谷 P1348 Couple number
题目描述 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple number.你的工作就是判断一个数N是不是Couple number. 输入输出格式 输入格式 ...
- RSAROLL
题目:http://www.shiyanbar.com/ctf/1918 # -*- coding: utf-8 -*- import gmpy2 ciper = [704796792, 752211 ...
- 用循环链表实现Josephus问题
Josephus问题:设有n个人围坐在一个圆桌周围,现从第s个人开始报数,数到第m的人出列,然后从出列的下一个人重新开始报数,数到第m的人又出列.如此反复直到所有的人全部出列为止. 思路:构建一个没有 ...