Question

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

Solution

这道题用穷举法求解,时间复杂度为O(n^2)。但是要注意几个细节,如果包含的点数小于等于2,那么直接返回点数。

如果第三个点和前面两个点中的一个相等,那么直接在总数上累加1。如果第三个点和前面两个点的横坐标都一样,那么直接在总数上累加1,如果只和其中一个一样,那么直接计算下一个点。如果两个都不一样,那么就开始计算斜率是否相等,相等的话,总数就累加1,反之。

Code

/**
* 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) {
if (points.size() <= 2)
return points.size(); int maxNumbers = 2;
for (int i = 0; i < points.size(); i++) {
for (int j = i + 1; j < points.size(); j++) {
int count = 2;
for (int k = 0; k < points.size(); k++) {
if (k == i || k == j)
continue;
// 重叠
if ((points[k].x == points[i].x && points[k].y == points[i].y) ||
(points[k].x == points[j].x && points[k].y == points[j].y)) {
count++;
if (count > maxNumbers)
maxNumbers = count;
continue;
} // 横坐标一样,相减为0,不能计算斜率
if (points[k].x == points[j].x) {
if (points[j].x == points[i].x) {
count++;
if (count > maxNumbers)
maxNumbers = count;
continue;
} else
continue;
} else if (points[j].x == points[i].x) {
continue;
} // 计算斜率
if ((points[k].y - points[j].y) / (float)(points[k].x - points[j].x) ==
(points[j].y - points[i].y) / (float)(points[j].x - points[i].x))
count++;
if (count > maxNumbers)
maxNumbers = count;
}
}
}
return maxNumbers;
}
};

LeetCode——max-points-on-a-line的更多相关文章

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

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

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

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

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

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

  7. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

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

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

  10. [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. 瀑布流 jquery。

    本人小菜鸟一仅仅,为了自我学习和交流PHP(jquery,linux,lamp,shell,javascript,server)等一系列的知识,小菜鸟创建了一个群. 希望光临本博客的人能够进来交流. ...

  2. Retrofit学习笔记(一)

    github上的介绍,简单明了 Type-safe HTTP client for Android and Java by Square, Inc. http://square.github.io/r ...

  3. [Spring MVC]学习笔记--form表单标签的使用

    github例子地址: https://github.com/lemonbar/spring-mvc-jsp 效果图 关于spring mvc的标签的讲解, 有一篇blog已经讲的很细了. http: ...

  4. [NOIP2018TG]赛道修建

    [NOIP2018TG]赛道修建 考场上multiset调不出啊啊啊!!! 首先肯定是二分答案 做树形dp,f[i]表示i点的子树两两匹配后剩下的最长长度 匹配可以用multiset维护 但是菊花图跑 ...

  5. LOJ#2230. 「BJOI2014」大融合

    LOJ#2230. 「BJOI2014」大融合 题目描述 小强要在$N$个孤立的星球上建立起一套通信系统.这套通信系统就是连接$N$个点的一个树.这个树的边是一条一条添加上去的. 在某个时刻,一条边的 ...

  6. 洛谷P5274 优化题(ccj)

    洛谷P5274 优化题(ccj) 题目背景 CCJCCJ 在前往参加 Universe \ OIUniverse OI 的途中... 题目描述 有一个神犇 CCJCCJ,他在前往参加 Universe ...

  7. DOM 常见事件

    onclick //当用户点击某个对象时调用的事件句柄. ondblclick //当用户双击某个对象时调用的事件句柄. onfocus //元素获得焦点. onblur //元素失去焦点. 应用场景 ...

  8. (4.16)sql server迁移DB文件(同一DB内)

    SQL Server 修改数据库物理文件存在位置 关键词:迁移文件,迁移temp库(这怎么迁移呢,用方法2即可,需要重启实例) 三种均需要离线: 一共分为(1)脱机迁移和(2)在线迁移. (1)迁移方 ...

  9. IDEA创建Tomcat8源码工程流程

    上一篇文章的产出,其实离不开网上各位大神们的辅助,正是通过他们的讲解,我才对Tomcat的结构有了更进一步的认识. 但在描述前后端交互的过程中,还有很多细节并没有描述到位,所以就有了研究Tomcat源 ...

  10. 2.4 使用ARDUINO控制MC20进行GPS数据的获取和解析

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...