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 ...
随机推荐
- 02.centos6.4找不到ifcfg-eth0(静态ip配置)
1.默认情况在/etc/sysconfig/network-scripts/目录下面找不到ifcfg-eth0文件,我们需要手动copy 1.1动态ip配置 #cp ifcfg-lo ifcfg-et ...
- TCP/IP三次握手与四次挥手
三次握手: TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接 位码即tcp标志位 ...
- Vue router 的使用--初级
在说 VueRouter 之前,首先要弄明白vueRouter 是干什么的,有什么用 说出来其实很简单,就是一个模板替换的问题,当路由改变的时候,把和路由相关的模板显示出来,就是这么简单.但是,当我们 ...
- 初级java程序员-各公司技能要求
熟悉tomcat部署和性能调试,开发常用linux 命令,有性能调优(tomcat,sql等)经验优先: 熟练使用SSH.springmvc.mybatis.Hibernate.jquery等框架,了 ...
- 将IP地址字符串转为32位二进制
def str2bin(s): temp = s.split('.') result = '' for i in range(len(temp)): temp[i] = str(bin(int(tem ...
- SQLServer中利用NTILE函数对数据进行分组的一点使用
本文出处:http://www.cnblogs.com/wy123/p/6908377.html NTILE函数可以按照指定的排序规则,对数据按照指定的组数(M个对象,按照某种排序分N个组)进行分组, ...
- app与jvm 反向代理时config的设置(用于在web页面显示npm(就如tomcat)产生的页面)
dev: { // Various Dev Server settings contentBase: ROOT, host: ip, port: 8084, //此端口为任意设置,不重复即可,为 ...
- C++ 自定义控件的移植(将在其它程序中设计的自定义控件,移植到现在的系统中)
方法很简单就是将需要的代码 复制到 新系统中就可以了,方法就是 把相关文件添加到现有的系统中,并特别注意以下问题 \如果原设计中用到了菜单或是其它资源,相应的资源要在新的菜单中,手动添加. 目前没有发 ...
- Swift字符串常用方法
1.0 比较两个字符串是否相等 判断字符串相等的方法是: ==. var str1 = "Hello world" var str2 = "Hello world&quo ...
- js高级-数组的map foreach 方法
函数式编程 sort map forEach .... jQuery() 数组的sort 方法 传入一个匿名函数 就是函数式编程 ie9 以上的浏览器支持 map 方法 map方法 返回一个新数 ...