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

解题思路:题目说的非常清楚,寻找二维点在一条直线上的最大数目。

思路一:

  首先想到的方式就是直接法,遍历每个节点,一个节点下,遍历一遍,确定一条直线后,再遍历一遍确定这条直线上的次数,这样的话,时间复杂度就是O(n3)。感觉在OJ上会跪,就不写了//不过看到刘斌的博客,貌似是可以AC的

思路二:

  既然直接法太复杂的话,我们接着思考其他方法。我想到可以用直线的斜率k描述一条直线,把所有的点的直线搭配的斜率计算出来,存储起来,最后找到斜率出现最多的次数,就对应点最多的直线。

  考虑使用map,建立斜率k和出现次数的映射,最后取出最大的次数即可。其中需要处理,两个点相等以及斜率不存在(即横坐标相等的情况)

代码:

 1 class Solution {
 2 public:
 3     int maxPoints(vector<Point>& points) {
 4         map<double,int> slope;           //建立斜率与出现次数的映射
 5         if(points.size() <= 2)          //点的个数小于3时,直接返回点的个数
 6             return points.size();
 7          int n = points.size(),ans = 0;     //ans为最终结果
 8         for(int i = 0; i < n - 1; i++)
 9         {
10             int maxNum = 0;                //记录当前节点对应的斜率出现最多的次数
11             int same_x = 1;                //记录斜率不存在,即横坐标相等的情况
12             int samePoint = 0;             //记录重合的点
13             slope.clear();
14             for(int j = i + 1; j < n; j++)
15             {
16
17                   if (i == j)
18                       continue;
19                   if (points[i].x == points[j].x && points[i].y == points[j].y)
20                   {
21                     samePoint++;
22                     continue;
23                 }
24                   if (points[i].x == points[j].x)  //斜率不存在
25                   {
26                       same_x++;
27                       continue;
28                   }
29                   double k = (double)(points[i].y-points[j].y)/(points[i].x-points[j].x);
30                   if (slope.find(k) != slope.end())   //更新当前斜率出现的次数
31                       slope[k]++;
32                   else
33                       slope[k] = 2;
34             }
35             for (map<double,int>::iterator it = slope.begin(); it != slope.end(); ++it)   //找出最大次数的斜率
36                 same_x = max(same_x,it->second);
37             maxNum = same_x + samePoint;
38             ans = max(ans,maxNum);                  //与之前的最大值比较
39         }
40         return ans;
41     }
42 };

View Code

在leetcode上提交,可以AC,分析一下这个算法的复杂度,外层为两个循环,内层使用map求出最大值。时间复杂度为O(n2)。

此博客中的内容均为原创或来自网络,不用做任何商业用途。欢迎与我交流学习,我的邮箱:lsa0924@163.com

转载于:https://www.cnblogs.com/Jueis-lishuang/p/5040250.html

解题报告(LeetCode):Max Points on a Line相关推荐

  1. LeetCode: Max Points on a Line

    LeetCode: Max Points on a Line LeetCode: Max Points on a Line Given n points on a 2D plane, find the ...

  2. 解题报告: LeetCode Max Points on a Line

    题目出处:https://leetcode.com/submissions/detail/47640173/ 题意描述: 由于过于简洁,故不再翻译,具体描述见一下英文描述: Given n point ...

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

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

  7. 【重要+细节】LeetCode 149. Max Points on a Line

    LeetCode 149. Max Points on a Line Solution1: 参考花花酱:https://zxi.mytechroad.com/blog/geometry/leetcod ...

  8. leetcode 149. Max Points on a Line |149. 直线上最多的点数(Java)

    题目 https://leetcode.com/problems/max-points-on-a-line/ 题解 hard 题,普通解法不难,有几个小坑: key : key : value 的存储 ...

  9. LeetCode之Max Points on a Line Total

    1.问题描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

  10. 【leetcode】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. ...

最新文章

  1. Securing Session State
  2. mysql max和order by_mysql – 为什么MAX()比ORDER BY慢100倍… LIMIT 1?
  3. 制作精美的网站首页模板应该如何操作?
  4. 基于WINCE6.0+S3C6410通过USB下载stepldr
  5. 域名服务器的配置文档,配置自己的域名服务器
  6. Bootstrap常用类
  7. OD使用教程3(中) - 调试篇03|解密系列
  8. MyBatis-动态sql语句-if用法——MySQL系列学习笔记
  9. nextcloud icon_吉利ICON,我信任的好伙伴,感谢有你的陪伴
  10. 设计一个具有等待队列的连接池
  11. JAVA翻译官_Java开发笔记(三)Java帝国的特种官吏
  12. php mysql倒计时_php 倒计时程序
  13. 代替嵌套循环java_蓝石榴_个人博客_Java中for循环嵌套的替换优化
  14. Windows提权 cmd 开启 3389
  15. VMware与xshell安装教程
  16. 【电信学】【2019.07】基于ATOLL的5G网络规划与优化
  17. 初中毕业也能月薪过万!5个质量极高的教程网站,免费献给你
  18. npm WARN deprecated bfj-node4@5.3.1: Switch to the `bfj` package for fixes and new features
  19. 自定义数据集算子数据结构
  20. 2012春晚节目清单:

热门文章

  1. 百度浏览器内核太低,浏览京东有问题
  2. php创建ceph桶,手动部署ceph jewel
  3. java中关于x轴翻转和y轴翻转如何计算_如何避免这8个常见的深度学习/计算机视觉错误?
  4. mysql用binlog回复_mysql binlog回复数据,亲测
  5. mysql pheonix hbase,mybatis连接phoenix操作hbase
  6. 开两个服务内存溢出_详解JVM内存区域
  7. Confluence 6 重构索引缓慢
  8. Vue折腾记 - (2)写一个不大靠谱的面包屑组件
  9. 【数据蒋堂】索引的本质是排序
  10. 条款40:慎重的选择多重继承