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 ...
随机推荐
- ubuntu下php xdebug的安装(配置)
首先Xdebug要和php版本对应,具体查看官网 https://xdebug.org/ xdebug-2.1.0PHP Version 5.3.10linux下解压xdebug包.1.进入xd ...
- Phonegap项目中禁用WebViewBounce
UIWebView是iOS SDK中一个最常用的控件,在PhoneGap中,默认也是使用UIWebView作为默认视图显示我们的HTML应用的. 在使用PhoneGap的项目中,默认WebView ...
- 4.mybatis属性和表的列名不相同时的处理方法
/** * 属性和表的列名不相同时的处理方法 * 1.sql中给列重新命名: * select tid id, tname name from teacher t where tid=#{id} * ...
- 不要温柔地走入AMD
1.无依赖情况 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- linux终端vi同时显示多个文件的分屏操作及切换操作
以前看到那边分屏操作的觉得很高端,现在初步整理了一下. 这里不是那个用代码实现的分屏,完全属于linux的操作命令 一.打开并显示文件 1.打开 这个不用说了,就是vi xx.c,或者vi xx1.c ...
- iOS - OC NSString 字符串
前言 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> @interface NSM ...
- js求和算法研究
如果遇到一个试题是这样. function sum(arr){ //求和 } 你会怎么做? 反正我第一反应是这样做. function sum(arr){ var result = 0; for(va ...
- poj1971Parallelogram Counting
链接 越来越感觉到了数学的重要性!.. 这题本来用以斜率和长度为key值进行hash不过感觉很麻烦还TLE了.. 最后知道中点一样的话就可以组成平行四边形,初中数学就可以了.. #include &l ...
- python利用xmlrpc方式对odoo数据表进行增删改查操作
# -*- encoding: utf-8 -*- import xmlrpclib #导入xmlrpc库,这个库是python的标准库. username ='admin' #用户登录名 pwd = ...
- 动态替换fragment
// [1]获取手机的宽和高 windommanager WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); in ...