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 ...
随机推荐
- jdbc 使用谨记
jdbc是java操作数据库的杀手锏.所有java程序员,对jdbc应该都不陌生. 但是,应该你也曾经被其折磨的抓耳挠腮,咬牙切齿吧,也许正因为这样你才对其记忆犹新,刻骨铭心. 这里有一些使用jdbc ...
- django 加日志
LOGGING = { 'version': 1, 'disable_existing_loggers': False, # 指定输出的格式,被handler使用. 'formatters': { ' ...
- Redis事物及锁的运用
redis与mysql事物比较如下: 下面是一个redis事物运用于买票的demo
- 洛谷——P2504 [HAOI2006]聪明的猴子
P2504 [HAOI2006]聪明的猴子 题目描述 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着,部分植物的树冠露在水面上. ...
- Django Template(模板系统)
一.Django模板 内置模板标签和过滤器 二.常用操作 两种特殊符号: {{ }} 和 {% %} 变量相关的用: {{ }} 逻辑相关的用: {% %} 2.1 变量 在Django的模 ...
- discourse论坛迁移
在源设备的操作备份数据文件tar -czvf discoursefile716.tar.gz /var/discourse然后把此discoursefile716.tar.gz文件传到需要迁移的设备上 ...
- django访问静态变量的设置
在项目的urls.py文件中 默认urlpatterns是空的列表需要填入url匹配的路由,默认使用static from django.conf.urls import include, url f ...
- 浏览器控制之 selenium,phantomJs谷无头浏览器
目录 浏览器控制之 selenium,phantomJs谷无头浏览器 selenium phantomJs 需求是尽可能多的爬取豆瓣网中的电影信息 谷歌无头浏览器 浏览器控制之 selenium,ph ...
- 【郑轻邀请赛 G】密室逃脱
[题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2133 [题意] [题解] 考虑每一个二进制数的最高位->第i位; 肯定是1(这 ...
- 洛谷 P4198 BZOJ 2957 楼房重建
题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些事件发生在一个 ...