题目原文:

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. mysql存储过程之遍历多表记录后插入第三方表中

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

  2. php字符串无乱码截取函数封装

    /** * * 中英混合字符串长度判断 * @param unknown_type $str * @param unknown_type $charset */ function strLength( ...

  3. Josephus problem(约瑟夫问题,丢手绢问题)

    约瑟夫问题 约瑟夫环问题是一个数学应用题:已知n个人(以编号1,2,3.....,n)围坐在一张圆桌的周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列 ...

  4. Linux如何查看端口(转)

    Linux如何查看端口 1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 # lsof -i:8000 COMMAND PID USER ...

  5. 2977,3110 二叉堆练习1,3——codevs

    二叉堆练习1 题目描述 Description 已知一个二叉树,判断它是否为二叉堆(小根堆) 输入描述 Input Description 二叉树的节点数N和N个节点(按层输入) 输出描述 Outpu ...

  6. 救济金发放(The Dole Queue, UVa 133)

    n(n<20)个人站成一圈,逆时针编号为1-n.有两个官员,A从1开始逆时针数,B从n开 始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个 官员停在同一个人上) ...

  7. uva 10048 Audiophobia UVA - 10048

    题目简介 一个无向正权图,求任意两个节点之间的路径里最短的路径长度. 直接Floyd解决,就是注意要把Floyd的DP式子改一下成 G[i][j]=min(G[i][j],max(G[i][k],G[ ...

  8. 使用redis和简单token机制校验身份的思路

    1. 登录时生成token, 以token为键,以用户信息为值,存储在redis中,设置key过期时间 2. 需要身份验证的接口,带上token 3. 接口校验redis中token是否存在 4. 存 ...

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

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

  10. webstorm+nodejs环境中安装淘宝镜像

    用过nodejs的人都知道,从node的官方模板库下载依赖包的时候,经常会遇到“假死”(页面静止不动)的状态,这种速度简直要逼死焦急地等待下班的人.还好咱们万能的淘宝提供了淘宝镜像这么一个不要更好用的 ...