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的更多相关文章

  1. 【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. ...

  2. Max Points on a Line(直线上最多的点数)

    给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o |     o | ...

  3. 【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 ...

  4. Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)

    Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...

  5. 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 ...

  6. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  7. [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. ...

  8. 哈希表(Hash Table)原理及其实现

    原理 介绍 哈希表(Hash table,也叫散列表), 是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映 ...

  9. 【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 ...

随机推荐

  1. 八皇后问题C语言解法

    偶遇八皇后问题,随即自己写了一个仅供参考 #include<stdio.h> #include<math.h> #define SIZE 8 void Circumsribe( ...

  2. UNITY2018 真机开启deepprofiling的操作

    手机上运行游戏并开启deepprofiling的命令如下 命令一:adb shell am start -n com.szyh.YHP1.kaopu/com.szyh.YHP1.kaopu.MainA ...

  3. Java解决小孩围圈问题

    问题描述:一堆小孩围成一个圈,从第一个小孩开始数,每数到第三个便把第三个孩子删去,数到只剩一个小孩为止,并求该孩子的具体编号. 解决办法 1. package test; public class C ...

  4. iOS中的MVC

      我们今天谈谈cocoa程序设计中的 模型-视图-控制器(MVC)范型.我们将从两大方面来讨论MVC: 什么是MVC? M.V.C之间的交流方式是什么样子的? 理解了MVC的概念,对cocoa程序开 ...

  5. C# HttpWebRequest 模拟下载

    C# web 获取服务端cookie 原文地址:https://www.cnblogs.com/louby/p/5569536.html C#多线程环境下调用 HttpWebRequest 并发连接限 ...

  6. 吴裕雄 python 机器学习-Logistic(1)

    import numpy as np def loadDataSet(): dataMat = [] labelMat = [] fr = open('D:\\LearningResource\\ma ...

  7. 学JS的心路历程-JS支持面向对象?(二)

    昨天讲了面向对象的继承,今天我们来谈谈多态和封装吧! 多态polymorphism 抽象讲法解释,就是使用单一界面操作多种型态的物件 继承父类别,定义与父类别中相同的方法,但实作内容不同,称为复写(o ...

  8. 三种文本特征提取(TF-IDF/Word2Vec/CountVectorizer)及Spark MLlib调用实例(Scala/Java/python)

    https://blog.csdn.net/liulingyuan6/article/details/53390949

  9. Applese的回文串-dfs

    链接:https://ac.nowcoder.com/acm/contest/330/I来源:牛客网 题目描述 自从 Applese 学会了字符串之后,精通各种字符串算法,比如……判断一个字符串是不是 ...

  10. linux下网络配置

    配置相关 http://bbs.acehat.com/thread-813-1-1.html