题目原文:

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语句整合

    常用MySQL语句整合 1. MySQL服务的配置和使用 修改MySQL管理员的口令:mysqladmin –u root password 密码字符串 如:mysqldmin –u root pas ...

  2. C# 字符串的入门

    1."@"表示字符串中的"\"不当成转义符. 2.转义符或者特殊处理的一些字符只是针对于代码中直接写出的字符串中,对于程序运行中读取出来的转义符或者特殊处理的字 ...

  3. Centos 安装配置iscsi

    在测试oracle rac的时候用iscsi来模拟磁阵的(真的磁阵需要多路径软件),简单的记录下 #scsi server yum install scsi-target-utils service ...

  4. img图片加载出错处理

    img图片加载出错处理   为了美观当网页图片不存在时不显示叉叉图片 当在页面显示的时候,万一图片被移动了位置或者丢失的话,将会在页面显示一个带X的图片,很是影响用户的体验.即使使用alt属性给出了” ...

  5. Vue中this.$router.push参数获取(通过路由传参)【路由跳转的方法】

    传递参数的方法: 1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用nam ...

  6. swift 关键字willSet 和 didSet

    // 下面是苹果给出的解释,就是在给属性设置新值的时候,可以在设置前和设置后做一些处理,这两个关键字就好像对该属性变化的监控 If you don’t need to compute the prop ...

  7. Excel 导入 分批导入

    tm.begin(); sm.begin(); try { result = excel.readE(headKeyV,path); for (int index = 1; index < re ...

  8. How To:使用dmidecode获取机器序列号(Serial number)

    使用dmidecode可以获取关于机器的诸多信息,比如机器的序列号   [root@dc1db01 ~]# dmidecode -s system-serial-number   processor的 ...

  9. [LUOGU] 4933 大师

    \(Orz\) \(ljt12138!\) 设状态\(f[i][j]\)表示以\(i\)为结尾,公差为\(j\)的长度大于\(1\)的数列有几个. 然后转移方程就很好想了. \(k=H[i]-H[j] ...

  10. Python模块 os.walk

    Os.walk os.walk(top,topdown=True,onerror=None,followlinks=False) os.walk()是python中内置(built-in)的目录树生成 ...