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

  1. /**
  2. * Definition for a point.
  3. * struct Point {
  4. * int x;
  5. * int y;
  6. * Point() : x(0), y(0) {}
  7. * Point(int a, int b) : x(a), y(b) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int maxPoints(vector<Point> &points) {
  13. if (points.size() <= 2)
  14. return points.size();
  15. int maxNumbers = 2;
  16. for (int i = 0; i < points.size(); i++) {
  17. for (int j = i + 1; j < points.size(); j++) {
  18. int count = 2;
  19. for (int k = 0; k < points.size(); k++) {
  20. if (k == i || k == j)
  21. continue;
  22. // 重叠
  23. if ((points[k].x == points[i].x && points[k].y == points[i].y) ||
  24. (points[k].x == points[j].x && points[k].y == points[j].y)) {
  25. count++;
  26. if (count > maxNumbers)
  27. maxNumbers = count;
  28. continue;
  29. }
  30. // 横坐标一样,相减为0,不能计算斜率
  31. if (points[k].x == points[j].x) {
  32. if (points[j].x == points[i].x) {
  33. count++;
  34. if (count > maxNumbers)
  35. maxNumbers = count;
  36. continue;
  37. } else
  38. continue;
  39. } else if (points[j].x == points[i].x) {
  40. continue;
  41. }
  42. // 计算斜率
  43. if ((points[k].y - points[j].y) / (float)(points[k].x - points[j].x) ==
  44. (points[j].y - points[i].y) / (float)(points[j].x - points[i].x))
  45. count++;
  46. if (count > maxNumbers)
  47. maxNumbers = count;
  48. }
  49. }
  50. }
  51. return maxNumbers;
  52. }
  53. };

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. Codeforces Beta Round #25 (Div. 2)--A. IQ test

    IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  2. C#关于AutoResetEvent的使用介绍----修正

    说明 之前在博客园看到有位仁兄发表一篇关于AutoResetEvent介绍,看了下他写的代码,看上去没什么问题,但仔细看还是能发现问题.下图是这位仁兄代码截图. 仁兄博客地址:http://www.c ...

  3. <2013 07 05> 804.15. 4--> TI MSP430+CC2520 调试

    这一周,实际参与eCar项目的工作正式展开. 来TUM的第一个月,主要熟悉了eCar的机电结构,特别是熟悉了eCar的IT(Information Technology),包括硬件和代码. 来的时候, ...

  4. 内置函数:min 用法

    内置函数:min 用法 源码 def min(*args, key=None): # known special case of min """ min(iterable ...

  5. [转载]MySQL concat函数的使用

    MySQL concat函数是MySQL数据库中众多的函数之一,下文将对MySQL concat函数的语法和使用进行说明,供您参考和学习. MySQL concat函数使用方法:CONCAT(str1 ...

  6. github常用的git命令

    添加已有项目到github: touch README.md //新建说明文件 git init //在当前项目目录中生成本地git管理,并建立一个隐藏.git目录 git add . //添加当前目 ...

  7. App的开发过程

    不同的项目管理模式或许会有完全不同的流程步骤.但是专业性几乎是保证产品质量的唯一准则. App的开发过程主要分为以下阶段,本文会按顺序为大家简单地说明: 1.需求梳理.分析 2.产品原型图绘制 3.U ...

  8. Python常见序列详解

    一.Python中序列的分类 常见序列类型包括字符串(普通字符串和unicode字符串),列表和元组.所谓序列,即成员有序排列,可通过下标访问. 二.Python序列通用操作 下面我们将分别以字符串. ...

  9. python学习之路-第一天-接触python

    我的入门就决定用<简明Python教程> <简明Python教程> 1. python的优势 简单:专注于解决问题而不是关注语言本身 易学:容易上手 开源.免费 可移植性非常强 ...

  10. 曾经跳过的坑------replace、替换斜杠反斜杠、时间格式化处理

    JAVA 中: 坑一: replace没有用对象进行接收.直接使用 dateStr.replaceAll("\\/", "-"); 是不行的,至少得加上 &qu ...