题目: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.

这道题需要稍微转变一下思路,用斜率来实现,试想找在同一条直线上的点,怎么判断在一条直线上,唯一的方式也只有斜率可以完成,我开始没想到,后来看网友的思路才想到的,下面是简单的实现:
其中有一点小技巧,利用map<double, int>来存储具有相同斜率值的的点的数量,简洁高效。

 /*
Definition for a point
*/
// struct Point {
// int x;
// int y;
// Point():x(0),y(0) {}
// Point (int a, int b):x(0), y(0) {}
// }; int maxPoints(vector<Point> &points) {
if (points.empty())
return ;
if (points.size() <= )
return points.size(); int numPoints = points.size();
map<double, int> pmap; //存储斜率-点数对应值
int numMaxPoints = ; for (int i = ; i != numPoints - ; ++i) {
int numSamePoints = , numVerPoints = ; //针对每个点分别做处理 pmap.clear(); for (int j = i + ; j != numPoints; ++j) {
if(points[i].x != points[j].x) {
double slope = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x);
if (pmap.find(slope) != pmap.end())
++ pmap[slope]; //具有相同斜率值的点数累加
else
pmap[slope] = ;
}
else if (points[i].y == points[j].y)
numSamePoints ++; //重合的点
else
numVerPoints ++; //垂直的点 }
map<double, int>::iterator it = pmap.begin();
for (; it != pmap.end(); ++ it) {
if (it->second > numVerPoints)
numVerPoints = it->second;
}
if (numVerPoints + numSamePoints > numMaxPoints)
numMaxPoints = numVerPoints + numSamePoints;
}
return numMaxPoints + ;
}

LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard的更多相关文章

  1. [leetcode]Max Points on a Line @ Python

    原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...

  2. LeetCode:Max Points on a Line

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

  3. [LeetCode] Max Points on a Line 题解

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

  4. lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上

    题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...

  5. [LeetCode] 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: 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 ...

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

  8. [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 ...

  9. [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. ...

随机推荐

  1. 原子性: Interlocked 类

    public class CounterNoLock:CountBase { private int _count; public int Count { get { return _count; } ...

  2. idea2017启动ssm项目卡在build阶段后报outofmemory

    如上图,设置build process heap size(Mbytes)(构建过程堆大小(单位MB))为4000,即约4GB.之前设置的是700,修改之后问题解决. 补充:导入新项目后,此参数会初始 ...

  3. 业务数据实体(model) 需要克隆的方法

    业务数据实体(model) 需要克隆的时候 可以使用 Json.Deserialize<InquireResult>(Json.Serialize<InquireResult> ...

  4. Shell中read命令--学习

    read命令 -p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 1.基本读取read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后,read命令将数 ...

  5. BZOJ4377 Kurs szybkiego czytania \ Luogu 3589[POI2015]KUR - 数学思维题

    Solution 我又双叒叕去看题解啦$QAQ$, 真的想不到鸭 输入 $a$ 和 $n$ 互质, 所以满足 $a \times i \ mod \ n$ $(0<=i<n)$ 肯定是不重 ...

  6. 查看memcached进程

  7. leveldb 学习记录(一) skiplist

    leveldb LevelDb是一个持久化存储的KV系统,并非完全将数据放置于内存中,部分数据也会存储到磁盘上. 想了解这个由谷歌大神编写的经典项目. 可以从数据结构以及数据结构的处理下手,也可以从示 ...

  8. CPU与IRP的一些相关函数

    VOID KiAdjustIrpCredits ( VOID )其中 Number = KeNumberProcessors;Prcb = KiProcessorBlock[Index]; 多核情况下 ...

  9. 第一个VS2015 Xaramin Android项目(续)

    上文说到已经第一个 App已经可以运行,但是并不能调试! 经过细心发现,我察觉到VS刚开始进入了调试模式,但是一闪而过.也就是说调试失败了,此时需要等待一段时间才能打开此App,如果立即打开App 会 ...

  10. JS基础-数组的常用方法-冒泡排序

    1.数组  1.关联数组    以数字作为元素下标的数组,就是索引数组.    以字符串作为元素下标的数组,就是关联数组.  2.js的关联数组    ex:在php中       $array=[& ...