[LintCode] 最多有多少个点在一条直线上
/**
* 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:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
// Write your code here
unordered_map<float, int> slopes;
int maxp = , n = points.size();
for (int i = ; i < n; i++) {
slopes.clear();
int duplicate = ;
for (int j = i + ; j < n; j++) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
duplicate++;
continue;
}
float slope = (points[i].x == points[j].x) ? INT_MAX:
(float)(points[i].y - points[j].y) / (points[i].x - points[j].x);
slopes[slope]++;
}
maxp = max(maxp, duplicate);
for (auto slope : slopes)
if (slope.second + duplicate > maxp)
maxp = slope.second + duplicate;
}
return maxp;
}
};
[LintCode] 最多有多少个点在一条直线上的更多相关文章
- lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...
- lintcode-186-最多有多少个点在一条直线上
186-最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...
- 一条直线上N个线段所覆盖的总长度
原文:http://blog.csdn.net/bxyill/article/details/8962832 问题描述: 现有一直线,从原点到无穷大. 这条直线上有N个线段.线段可能相交. 问,N个线 ...
- LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
题目:Max Points on a line Given n points on a 2D plane, find the maximum number of points that lie on ...
- [LintCode] Coins in a Line II 一条线上的硬币之二
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- 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. ...
- Lining Up(在一条直线上的最大点数目,暴力)
Lining Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- objectarx之判断三点是否在一条直线上
bool CCommonFuntion::IsOnLine(AcGePoint2d& pt1, AcGePoint2d& pt2, AcGePoint2d& pt3){ AcG ...
- [Swift]LeetCode149. 直线上最多的点数 | 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. ...
随机推荐
- Node.js开发入门—使用cookie保持登录
这次来做一个站点登录的小样例,后面会用到. 这个演示样例会用到Cookie.HTML表单.POST数据体(body)解析. 第一个版本号,我们的用户数据就写死在js文件中. 第二个版本号会引入Mong ...
- python3的pip管理器pip3
一且因为python2到3的痛苦升级,python3的pip程序也有一个别致的名字pip3 安装: apt-get install python3-pip 安装后不能直接使用pip,否则会提示没有安装 ...
- linux命令汇总1
允许非root用户使用“sudo” root身份登录系统,执行“visudo”,根据示例添加新的一个规则(记住输入的密码是当前用户密码,而不是root密码)#不需要密码执行sudo命令hadoop ...
- docker使用问题总结
1. docker报[Error response from daemon: Error running DeviceCreate (createSnapDevice) dm_task_run fai ...
- 【C语言】21-结构体
C语言的核心部分都说得七七八八了,相信大家已经对C语言的基本数据类型(char\int\float).数组.指针都很熟悉了,今天来学习C语言中另外一种数据类型:结构体.在iOS开发中,结构体是经常用到 ...
- 自己构造用于异步请求的JSON数据
有时候.serialize()或者.serializeJSON()莫名其妙的不能按照我们的要求将数据序列化. 或者其他什么问题然我们需要自己惊醒JSON数据的构造.因为js对JSON的支持做的比较好, ...
- KMP算法完整教程 (上)
KMP算法完整教程 全称: Knuth_Morris_Pratt Algorithm(KMP算法) 类型: 高级检索算法 功能: 字符串匹配查找 提出者: D.E.Knuth(克努兹),J.H.Mor ...
- cssText方式写入css
<div class="a" id="a">hello world</div> <script> //通过JS来覆写对象的样 ...
- AD smart pdf 中文丢失
Altium Designer将原理图通过smart pdf导出,原理图中的中文丢失了. 将原理图中的所有中文字体改为宋体即可. 百度知道上的也有说: 打开软件后,点击左上角的[DXP]→[Prefe ...
- 通过Hadoop安全部署经验总结,开发出以下十大建议,以确保大型和复杂多样环境下的数据信息安全。
通过Hadoop安全部署经验总结,开发出以下十大建议,以确保大型和复杂多样环境下的数据信息安全. 1.先下手为强!在规划部署阶段就确定数据的隐私保护策略,最好是在将数据放入到Hadoop之前就确定好保 ...