题目原文:

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. 170925_2 Python socket 创建UDP的服务器端和客户端

    [python版本]3.6 UDP服务器端: from socket import * from time import ctime host = '' port = 21567 buf_size = ...

  2. NSOperationQueue和GCD的区别,以及在什么场合下使用

    1> GCD是纯C语言的API .NSOperationQueue是基于GCD的OC的封装. 2> GCD只支持FIFO队列,NSOperationQueue可以方便设置执行顺序,设置最大 ...

  3. docker和jenkins安装启动

    docker安装Jenkins 1.安装Docker 1.1 yum 包更新到最新 sudo yum update 1.2 安装需要的软件包, yum-util 提供yum-config-manage ...

  4. P1002 过河卒 【递推、简单动规】

    题目描述 棋盘上AA点有一个过河卒,需要走到目标BB点.卒行走的规则:可以向下.或者向右.同时在棋盘上CC点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒 ...

  5. vmware 15安装破解及使用教程

    VMware Workstation Pro15虚拟机破解版(序列号+安装教程) VMware15已经推出,根据版本号名为VMware Workstation Pro 15是一款强大好用的桌面虚拟机软 ...

  6. 腾讯云,搭建Java开发环境

    搭建 JAVA 开发环境 任务时间:18min ~ 20min 此实验教大家如何配置 JDK .Tomcat 和 Mysql 安装 JDK JDK 是开发Java程序必须安装的软件,我们查看一下 yu ...

  7. 常用Git命令大全思维导图

    开发中代码管理少不了使用Git,对于初学者来说Git命令的学习是一个难过的坎,为了帮助大家记忆并快速掌握Git的基本使用,我把常用的Git命令整理成思维导图,分享给大家. 高清大图在线预览 http: ...

  8. C/C++ uchar的一个有趣用法

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51377490 图像处理中常常使用的一种 ...

  9. ESdata节点脱离集群,系统日志报120秒超时

    ES信息:Centos7.2,ES6.2.2 , MASTER:16核/128G物理 * 3 ,DATA:16核/128G/12块HDD6T组成RAID0 * 40, JVM开了30G,  目前只有一 ...

  10. hdu_1009_FatMouse' Trade_201310280910

    FatMouse' Trade http://acm.hdu.edu.cn/showproblem.php?pid=1009 Time Limit: 2000/1000 MS (Java/Others ...