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 ...
随机推荐
- Java——Socket编程(一)
1. 网络基础知识 两台机器之间需要进行通信,需要满足的条件: 每个机器有一个唯一的标识符(IP地址): 他们之间进行通信需要用同一种语言(协议): 每台主机上面有多个应用程序,如QQ,微博,迅雷等, ...
- Linux链接库一(动态库,静态库,库放在什么路径下)
http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http ...
- OB命令大全
CALC : 判断表达式 WATCH : 添加监视表达式 AT : 在指定地址进行反汇编 FOLLOW : 跟随命令 ORIG : ...
- CSS笔记(十三)CSS3之过渡
参考:http://www.w3school.com.cn/css3/css3_transition.asp 通过 CSS3,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元 ...
- 关于版本号:alpha、beta、rc、stable
定义好版本号,对于产品的版本发布与持续更新很重要: 但是对于版本怎么定义,规则如何确定,却是千差万别.具体应用,可以结合自己目前的实际情况命名: 很多软件在正式发布前都会发布一些预览版或者测试版,一般 ...
- tiled工具使用
转的 在这个分为上下两部分的教程中,我们将介绍如何使用Cocos2D-X和地图编辑器做一款基于地图块的游戏.在这个简单的地图块游戏里,一个精灵将在沙漠里搜寻它可口的西瓜! 在教程的第一部分,我们将介绍 ...
- Maven常用命令(转)
Maven库: http://repo2.maven.org/maven2/ Maven依赖查询: http://mvnrepository.com/ Maven常用命令: 1. 创建Maven的普通 ...
- Oracle中synonym和index
笔记: Oracle-同义词--通过用户名(模式名).表名 --授权:grant create synonym to test1(system用户下授权)) --私有 creat ...
- iOS - Swift SingleClass 单例类
前言 单例对象能够被整个程序所操作.对于一个单例类,无论初始化单例对象多少次,也只能有一个单例对象存在,并且该对象是全局的,能够被整个系统访问到. 单例类的创建 1.1 单例类的创建 1 单例类的创建 ...
- Java 中使用 UEditor 整理【待续。。。】
1.简介 官网:http://ueditor.baidu.com/website/index.html 演示:http://ueditor.baidu.com/website/examples/ 2. ...