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 ...
随机推荐
- day01-Python输出
输出 用print加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world'>>>print 'hello, world' print语句也可以跟上多个字符串,用 ...
- Java中Asm包有什么用?
ASM能做什么 我们都知道,一般情况下,Class文件是通过javac编译器产生的,然后通过类加载器加载到虚拟机内,再通过执行引擎去执行. 现在我们可以通过ASM的API直接生成符合Java虚拟机规范 ...
- C语言基础入门
搭建Windows平台C/C++开发环境 第1步: 第2步: 第3步: 第4步: 第5步: 第6步: 第7步: 第8步: 第9步: 第10步: 第11步: 第12步: 第13步: 第14步: 第15步 ...
- PRC远程过程调用
RPC(Remote Promote Call) 一种进程间通信方式.允许像调用本地服务一样调用远程服务. RPC框架的主要目标就是让远程服务调用更简单.透明.RPC框架负责屏蔽底层的传输方式(TCP ...
- Python使用xlwt模块 操作Excel文件
导出Excel文件 1. 使用xlwt模块 import xlwt import xlwt # 导入xlwt # 新建一个excel文件 file = xlwt.Workbook() # ...
- LeetCode OJ 15. 3Sum
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- rhel7磁盘管理
一.MBR主引导记录 MBR有512个字节,分为三个部分:第一部分446个字节,存储了引导分区:第二部分64字节为分区表:第三部分2个字节结束符:每个分区需16个字节,所以MBR的模式 ...
- is not writable or has an invalid setter method错误的解决
java中在配置spring时,遇到is not writable or has an invalid setter method的错误一般是命名方式的问题 需要写成private userInfoD ...
- vb的VSFlexGrid控件
多行选中 VSFlexGrid的SelectionMode = flexSelectionListBox,现在可以配合Ctrl进行多行选择 循环取值 用vsflexgrid.SelectedRows ...
- WIN7系统 如何上传文件到FTP服务器中
https://zhidao.baidu.com/question/214644671.html