题目原文:

Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subquadratic algorithm to count the number of points that are contained both in array a[] and array b[].

题目的目标就是计算重复point的个数,很简单,代码如下

 import java.awt.Point;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set; import edu.princeton.cs.algs4.StdRandom; public class PlanePoints {
private Set<Point> s = new HashSet<Point>();
private int samePointsNum;
PlanePoints(int n,Point[] inputa, Point[] inputb){
for(int i=0;i<n;i++){
s.add(inputa[i]);
s.add(inputb[i]);
}
samePointsNum = 2*n - s.size();
} public int samePointsNum(){
return samePointsNum;
} public static void main(String[] args){
int n = 10;
Point[] a = new Point[n];
Point[] b = new Point[n];
System.out.println(a.length);
for(int i=0;i<n;i++){
a[i] = new Point();
a[i].setLocation(StdRandom.uniform(n), StdRandom.uniform(n));
b[i] = new Point();
b[i].setLocation(StdRandom.uniform(n), StdRandom.uniform(n));
}
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
PlanePoints pp = new PlanePoints(n,a,b);
System.out.println(pp.samePointsNum);
}
}

Coursera Algorithms week2 基础排序 练习测验: Intersection of two sets的更多相关文章

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

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

  2. Coursera Algorithms week2 基础排序 练习测验: Permutation

    题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...

  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. 【译】x86程序员手册18-6.3.1描述符保存保护参数

    6.3 Segment-Level Protection 段级保护 All five aspects of protection apply to segment translation: 段转换时会 ...

  2. 体验SqlServer Express 2014

    想使用SQLServer Express记录一些数据,但使用起来并不令人愉快.SQLServer Express是一个免费的可用数据库,但似乎设置了一些门槛,多少显得并不真心实意.抛开版本(技术)限制 ...

  3. abstract class和interface 抽象类与接口类的区别

    抽象类与类型定义相关: 接口类与行为规范相关: 接口类不是类型. 抽象类:是不完整的类,函数实现未定义:可以继承,不可以实例化. 接口类:接口类不是类:是类间交互的规范:不能继承.不能实例化,只能实现 ...

  4. mysql存储过程之遍历多表记录后插入第三方表中

    自从学过存储过程后,就再也没有碰过存储过程,这是毕业后写的第一个存储过程. 因为项目里设备的种类比较多,分别存在不同的数据表中,java中对应不同的java bean对象,想要统一管理有点困难.最近正 ...

  5. UGUI世界坐标转换为UI本地坐标(游戏Hud的实现)

    实现世界坐标的原理是: 世界坐标和UGUI的坐标分属两个坐标系,他们之间是无法进行转换的,需要通过屏幕坐标系来进行转换(因为屏幕坐标是固定的),即先将游戏场景中的世界坐标通过游戏场景Camera转化为 ...

  6. 北京Linux运维培训怎么选?

    北京的地理优势和经济优势基本无需多言,作为全国机会最多的地方,吸引了无数的北漂前赴后继.作为中国互联网中心之一,北京有海量的运维岗位正在等待大家淘金.北京的Linux云计算培训业蓬勃发展. 云计算早已 ...

  7. mitmproxy安装与使用

    mitmproxy安装与使用 (抓包,中间人代理工具.支持SSL) 在开发微信公端的时候开发调试只能用浏览器自带开发工具,本来移动端可以用用fiddler.wireshark等工具来抓包,但是自从改用 ...

  8. Xpath语法与lxml库

    1. Xpath 1 )什么是XPath? xpath(XML Path Language)是一门在XML和HTML文档中查找信息的语言,可用来在XML和HTML文档中对元素和属性进行遍历. 2) X ...

  9. Coefficient Computation (大整数、Java解决)

    Coefficient Computation UVALive8265 题意:计算组合数C(n,k)的值并将值按给定的进制输出. 思路:Java大整数类硬上. PS:刚刚学完Java的大整数类,结果却 ...

  10. 二、Scrapy命令行工具

    本文转载自以下链接:https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/commands.html Scrapy是通过 scrapy 命令行工具 ...