Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

/**
* 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 n = points.size(), i, j, ans = ;
if( == n)
return ;
for(i = ; i < n; i++)
{
map<double, int> m;
map<int, int> x, y;
int samePoint = ;
for(j = ; j < n; j++)
{
int deltaX = points[i].x - points[j].x;
int deltaY = points[i].y - points[j].y;
if( == deltaX && == deltaY)
{
samePoint++;
}
else if( == deltaX)
{
if(x.find(points[i].x) == x.end())
x[points[i].x] = ;
else
x[points[i].x]++;
}
else if( == deltaY)
{
if(y.find(points[i].y) == y.end())
y[points[i].y] = ;
else
y[points[i].y]++;
}
else
{
double t = 1.0 * deltaX / deltaY;
if(m.find(t) == m.end())
m[t] = ;
else
m[t]++;
}
}
ans = max(ans, samePoint);
for(map<double, int>::iterator it = m.begin(); it != m.end(); it++)
ans = max(ans, it->second+samePoint);
for(map<int, int>::iterator it = x.begin(); it != x.end(); it++)
ans = max(ans, it->second+samePoint);
for(map<int, int>::iterator it = y.begin(); it != y.end(); it++)
ans = max(ans, it->second+samePoint);
}
return ans;
}
};

149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数的更多相关文章

  1. leetcode 149. Max Points on a Line --------- java

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

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

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

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

  4. [LeetCode] 149. 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. ...

  5. [leetcode]149. 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. ...

  6. 【LeetCode】149. Max Points on a Line 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...

  7. Java for LeetCode 149 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. 149. Max Points on a Line

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  9. 149. Max Points on a Line同一条线上的最多点数

    [抄题]: Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

随机推荐

  1. React生命周期浅析

    引言 关于React的生命周期API,官网,有着详细说明.但在实际写代码的过程中,这些说明不能解决所有的疑惑. 所以我列举了一些编码中常见用例,供大家参考. 示例代码如下 /* use case 1. ...

  2. Ubuntu 通过Deb安装 MySQL5.5(转载)

    1. 下载 MySQL 5.5 deb 安装包 cd /usr/local/src sudo wget -O mysql-5.5.22-debian6.0-i686.deb http://dev.my ...

  3. NPN&PNP

    一.晶体管基础知识 晶体管分2种:NPN.PNP 晶体管通常封装为TO-92,下面是元件实物图 和 元件符合: NPN: 当电压和电流被加到基极上时,NPN晶体管: 其工作原理: 就像水龙头—给控制开 ...

  4. 让你快速搭建一个bootstrap页面

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  5. C++ ASSERT() 断言机制

    C++ ASSERT() 断言机制 ASSERT()是一个调试程序时经常使用的宏,在程序运行时它计算括号内的表达式,如果表达式为FALSE (0), 程序将报告错误,并终止执行.如果表达式不为0,则继 ...

  6. Win7_SendTo文件夹

    Win7系统浮动sendto目录现在被移到了这里 : %APPDATA%\Microsoft\Windows\SendTo %APPDATA%是个环境变量,具体来说是在这里: C:\users\??? ...

  7. jj前端项目1th总结

    1:设计图--->分出几个独立模块--->颗粒化布局--->文档流控制整体布局--->固定位置的元素绝对定位,段落这种元素不可绝对定位.----->加上和后台交互用的js ...

  8. MyISAM与InnoDB的索引实现

    1.MyISAM 使用B+Tree 作为索引结构,叶子节点的data存放指针,也就是记录的地址.对于主键索引和辅助索引都是一样的.2.InnoDB 也使用B+Tree作为索引结构,也别需要注意的是,对 ...

  9. maven之详解继承与聚合

    说到聚合与继承我们都很熟悉,maven同样也具备这样的设计原则,下面我们来看一下Maven的pom如何进行聚合与继承的配置实现. 一.为什么要聚合? 随着技术的飞速发展和各类用户对软件的要求越来越高, ...

  10. commons-logging日志系统

    日志的重要性是随着系统的膨胀而显现的,在一个庞大的系统中查错没有各种日志信息    是寸步难行的.所以在系统加入日志是必须的. 最原始的日志方式,就是在程序的适当地方添加System.out.prin ...