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

题意:平面上给出n个点,求在同一条直线上的最多点。

思路:主要是用斜率。但是注意,水平和竖直方面上的。水平方向的斜率为0,但是浮点计算时,会出现0.0 和-0.0的问题,所以在计算水平方向时,判断y是否相等。

代码如下:

class Point {int x;int y;Point() { x = 0; y = 0; }Point(int a, int b) { x = a; y = b; }
}class Solution
{public int maxPoints(Point[] points){int result = 0;Map<Double, Integer> hm = new HashMap<Double, Integer>();for (int i = 0; i < points.length; i++){int samepoint = 1;int samex = 0;int samey = 0;for (int j = i + 1; j < points.length; j++){if (points[i].x == points[j].x && points[i].y == points[j].y){samepoint++;}else if (points[i].x == points[j].x){samex++;}else if (points[j].y == points[i].y){samey++;}else{double k = (double)(points[j].y - points[i].y) / (double)(points[j].x - points[i].x);if (hm.containsKey(k)){hm.put(k, hm.get(k) + 1);}else{hm.put(k, 1);}}}result = Integer.max(result, samepoint + samex);result = Integer.max(result, samepoint + samey);for (Double d : hm.keySet()){result = Integer.max(result, samepoint + hm.get(d));}hm.clear();}return result;}
}

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

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

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

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

  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 |149. 直线上最多的点数(Java)

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

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

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

最新文章

  1. scala 处理 Response返回 时间报错问题
  2. T-SQL查询进阶--流程控制语句
  3. Android下集成Paypal支付
  4. Javascript实现浏览器菜单命令
  5. 字符串表达式求值 C#实现
  6. 两个map中的数据,按照相同键,将所对应的值相加方法
  7. [react] 同时引用这三个库react.js、react-dom.js和babel.js它们都有什么作用?
  8. java学习(175):method类反射机制
  9. Android之仿网易V3.5新特性
  10. Linaro GCC 交叉编译工具链 国内源下载列表 (持续更新)
  11. Core Data的使用(二)备
  12. Immutable 操作在 React 中的实践
  13. 5A通过PMP考试分享
  14. 图形学基础|深度缓冲(DepthBuffer)
  15. ubuntu1604安装网卡驱动 联想g510
  16. Tomcat安装及环境配置(详细教程)
  17. paper的经验和会议排名
  18. C# WPF 3DTools下的TrackballDecorator清除view,出现Null异常
  19. Stone教程:一行代码就可以把3D场景植入到普通网页中
  20. 隐私空间伪装计算机,隐私空间app(文件夹隐藏) 6.1.9 免root

热门文章

  1. 杭电 HOJ 2553 N皇后问题 解题报告
  2. python之simplejson,Python版的简单、 快速、 可扩展 JSON 编码器/解码器
  3. 【身边的经济学】节约打印用纸
  4. 基于python物流管理系统毕业设计-Python程序设计实验报告一 :熟悉IDLE和在线编程平台...
  5. python画动图-Python绘制动态水球图过程详解
  6. php7和python3性能对比-python2.7和3.7的区别
  7. python 加注拼音-又一个奇葩要求,Python是如何将“中文”转“拼音”的?
  8. python爬虫教程i-Python 爬虫速成教程,还有35个实战项目送给你!
  9. 30岁自学python找工作-程序员自学Python开发,20到30岁几乎决定了你的未来!
  10. python怎么样才算入门编程-新手如何入门Python编程