Coursera Algorithms week2 基础排序 练习测验: Intersection of two sets
题目原文:
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的更多相关文章
- Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...
- Coursera Algorithms week2 基础排序 练习测验: Permutation
题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...
- 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 ...
随机推荐
- CentOS7配置VSFTP服务器
[1] 安装VSFTP [root@localhost ~]# yum -y install vsftpd [2] 配置vsftpd.conf文件 [root@localhost ~]# vi /et ...
- ubuntu 更改终端颜色
1.$ sudo gedit .bashrc 2.PS1="\[\033[1;36;01m\]\u\[\033[00m\]\[\033[1;34;01m\]@\[\033[00m\]\[\0 ...
- CAD当前选择实体发生变化调用事件(com接口)
主要用到函数说明: _DMxDrawXEvents::SelectModified 当前选择实体发生变化,会调用该事件,详细说明如下: 参数 说明 IDispatch* pAryId 当前被选择的实体 ...
- vim三种模式
一般模式 以vi打开一个文件就直接进入一般模式了.一般模式下可以移动光标查看内容,通过ESC回到一般模式. 一般模式下常用的操作: 撤销与重做 命令 说明 u 复原上一个操作 . 小数点 重复上一个操 ...
- Ansible 利用playbook批量部署Nginx
我这里直接部署的,环境已经搭建好,如果不知道的小伙伴可以看上一遍ansible搭建,都写好了,这里是根据前面环境部署的 192.168.30.21 ansible 192.168.30.25 ...
- c++ map: 根据value逆向查找key
#include <iostream> #include <map> #include <algorithm> #include <vector> #i ...
- js 随机数范围
Math.floor(Math.random()*(high-low+1) +low)
- NSE入门--nmap 脚本基础
- time、datatime模块
python中时间日期格式化符号 %Y 年份(4位数表示) %y 年份(2位数表示) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数 %I 12小时制小时数 %M 分 ...
- Laravel实用小功能
Laravel实用小功能 1.控制访问次数 laravel5.2的新特性,通过中间件设置throttle根据IP控制访问次数 原理:通过回传三个响应头X-RateLimit-Limit,X-RateL ...