Max Points on a Line (HASH TABLE
QUESTION
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
1ST TRY
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
int maxPoints(vector<Point> &points) {
int ret = ;
float slope;
unordered_map<float, int> slopeCounter;
for(int i = ; i < points.size(); i++)
{
for(int j= i+; j < points.size(); j++)
{
slope = (points[j].y-points[i].y)/(points[j].x-points[i].x);
slopeCounter[slope]++;
if(slopeCounter[slope] > ret) ret = slopeCounter[slope];
}
}
return ret;
}
};
Result: Runtime Error
Last executed input: [(0,0),(0,0)]
2ND TRY
注意了除数不能为0
class Solution {
public:
int maxPoints(vector<Point> &points) {
if(points.empty()) return ; int ret = ;
float slope;
unordered_map<float, int> slopeCounter;
int counter = ; for(int i = ; i < points.size(); i++)
{
for(int j= i+; j < points.size(); j++)
{
if(points[j].x-points[i].x==)
{
counter++;
continue;
}
slope = (points[j].y-points[i].y)/(points[j].x-points[i].x);
slopeCounter[slope]++;
if(slopeCounter[slope] > ret) ret = slopeCounter[slope];
}
}
return +max(ret,counter);
}
};
Result: Wrong
Input: [(0,0),(-1,-1),(2,2)]
Output: 4
Expected: 3
3RD TRY
考虑一条直线经过的点有重复计算
class Solution {
public:
int maxPoints(vector<Point> &points) {
if(points.empty()) return ; int ret = ;
int tmpMax = ;
float slope;
unordered_map<float, int> slopeCounter;
int verticalCounter = ; for(int i = ; i < points.size(); i++)
{
for(int j= i+; j < points.size(); j++)
{
if(points[j].x-points[i].x==) verticalCounter++;
else
{
slope = (points[j].y-points[i].y)/(points[j].x-points[i].x);
slopeCounter[slope]++;
}
}
tmpMax = verticalCounter;
for(unordered_map< float,int >::iterator it=slopeCounter.begin(); it!=slopeCounter.end();it++)
{
tmpMax =max(tmpMax, it->second);
}
ret = max(ret, tmpMax);
slopeCounter.clear(); //clear map, for line through point[i] is done.
verticalCounter = ;
} return +max(ret,verticalCounter);
}
};
Result: Wrong
Input: [(0,0),(1,1),(0,0)]
Output: 2
Expected: 3
4TH TRY
考虑有重叠点的情况
class Solution {
public:
int maxPoints(vector<Point> &points) {
if(points.empty()) return ; int ret = ;
int tmpMax = ;
float slope;
unordered_map<float, int> slopeCounter;
int verticalCounter = ;
int repCounter = ;
int i,j; for(i = ; i < points.size(); i++)
{
for(j = ; j < i; j++)
{
if(points[j].x==points[i].x && points[j].y==points[i].y) break;
}
if(j < i) continue;
for(j= i+; j < points.size(); j++)
{
if(points[j].x==points[i].x && points[j].y==points[i].y) repCounter++;
else if(points[j].x==points[i].x) verticalCounter++;
else
{
slope = (float)(points[j].y-points[i].y)/(points[j].x-points[i].x); //必须要有float,否则计算结果是整数
slopeCounter[slope]++;
}
}
tmpMax = verticalCounter;
for(unordered_map< float,int >::iterator it=slopeCounter.begin(); it!=slopeCounter.end();it++)
{//traverse map
tmpMax =max(tmpMax, it->second);
}
ret = max(ret, tmpMax+repCounter);
slopeCounter.clear(); //clear map, for line through point[i] is done.
verticalCounter = ;
repCounter = ;
}
return ret+;
}
};
Result: Accepted
Max Points on a Line (HASH TABLE的更多相关文章
- 【leetcode】Max Points on a Line(hard)☆
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- Max Points on a Line(直线上最多的点数)
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- 【leetcode】Max Points on a Line
Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...
- Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)
Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...
- LeetCode: Max Points on a Line 解题报告
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- [LintCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 哈希表(Hash Table)原理及其实现
原理 介绍 哈希表(Hash table,也叫散列表), 是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映 ...
- 【LeetCode】149. Max Points on a Line
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
随机推荐
- Spring Cloud Config中文文档
https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...
- MySQL创建只读账号
应用场景:只要公司有数据团队的,那免不了让这帮家伙把全公司的数据库数据都摸一遍,但是要是直接把root用户给了他们,未免有点危险,于是只能给这帮人设权限,一般而言,他们只是做读操作,既然做读操作,那么 ...
- elasticsearch-java
elastissearch的JAVA客户端 官网 java api文档 https://www.elastic.co/guide/en/elasticsearch/client/java-api/ ...
- Oracle保留两位小数的函数
1.最终保存成字符串类型 使用to_char()函数 // 其9代表:如果存在数字则显示数字,不存在则显示空格 // 其0代表:如果存在数字则显示数字,不存在则显示0,即占位符 // 其FM代表:删除 ...
- RabbitMQ系列教程之七:RabbitMQ的 C# 客户端 API 的简介(转载)
RabbitMQ系列教程之七:RabbitMQ的 C# 客户端 API 的简介 今天这篇博文是我翻译的RabbitMQ的最后一篇文章了,介绍一下RabbitMQ的C#开发的接口.好了,言归正传吧. N ...
- 如何用java读取properties文件
1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集.不过Properties有特殊的地 ...
- pytest 单元测试
pytest简介 pytest 是python的一种单元测试框架,它非常的简洁.清晰. pytest 安装 pip install -U pytest 查看pytest版本 pytest --vers ...
- vue练习
<div id="app"> <div> <span>姓名</span> <input type="text&quo ...
- ubuntu16.04 64bit 升级到 python3.6
https://blog.csdn.net/zhao__zhen/article/details/81584933 https://www.codetd.com/article/1967538 htt ...
- 环境变量之path的一点理解
最初安装java环境时一直不明白为什么要配置环境变量,百度了一下还是理解不透彻. 后来安装python时也要配置环境变量.. 在经过未配置和配置的操作后,才有些理解path的含义. 1.未配置环境变量 ...