149. 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.
/**
* 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* 求点集中在一条直线上的最多点数的更多相关文章
- 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. ...
- 149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...
- 【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 ...
- [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. ...
- [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. ...
- 【LeetCode】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- 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. ...
- 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 ...
- 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 ...
随机推荐
- js parseInt();parseFloat;Number()
1: parseInt( numString [, radix ] ) [测试浏览器:chromium && firefox] ①parseInt()函数用于将字符串转换为(十进制) ...
- PythonOCC 3D图形库学习—导入STEP模型
PythonOCC comes with importers/exporters for the most commonly used standard data files format in en ...
- 关于STM32库中 __IO 修饰符(volatile修饰符,反复无常的意思)
STM32例子代码中会有像这样的代码 static __IO uint32_t TimingDelay; 这里边的__IO修饰符不好理解,单从字面可以看出是为IO相关,查其标准库可以得知这个__IO ...
- ubuntu下解压zip文件乱码
安装unar 即可解决问题 sudo apt get install unar
- [SAP ABAP开发技术总结]EXIT-COMMAND
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- asp.netMVC4(基础知识----传值问题分析)
(1)一般在数据交互的时候,都会涉及到前后台间的相互传值,一般的情况下,方法也有多种,下面就后台定义变量往前台传值: 以下是后台代码: /// <summary> /// 展示举报信息 / ...
- iOS - UIControl
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIControl : UIView @available(iOS 2.0, *) public class UIC ...
- js的小随笔
1.在js中{ }中的块级语句没有独立的作用域 var i = 5;for(; i < 8; i++){ console.log(i); } //输出 5 6 7 //全局设置的变量i在for ...
- Android批量图片加载经典系列——采用二级缓存、异步加载网络图片
一.问题描述 Android应用中经常涉及从网络中加载大量图片,为提升加载速度和效率,减少网络流量都会采用二级缓存和异步加载机制,所谓二级缓存就是通过先从内存中获取.再从文件中获取,最后才会访问网络. ...
- iphone 使用技巧
http://www.app111.com/doc/100147120_1.html(隐藏某个图标) (3)传视频 moliplayer 和itunes ---应用(在下部)找到moliplaye ...