题目

https://leetcode.com/problems/max-points-on-a-line/

题解

hard 题,普通解法不难,有几个小坑:

  • key : key : value 的存储选型
  • double 作为 key 时的精度问题(改为用 String 存储 7位精度)
  • double 的正负 0 问题
  • double 的正负 Infinite 问题
  • 统计个数 sum 与排列组合 C 的转换
class Solution {public int maxPoints(int[][] points) {Map<String, Map<String, Integer>> kMap = new HashMap<>(); // y=kx+b,map存k:b:countint max = 0;for (int i = 0; i < points.length - 1; i++) {for (int j = i + 1; j < points.length; j++) {Double deltaY = (double) points[j][1] - points[i][1];Double deltaX = (double) points[j][0] - points[i][0];Double k = deltaY / deltaX;Double b = points[i][1] - k * points[i][0];if (k == 0) k = 0.0; // +0.0 -0.0 统一if (points[i][0] == points[j][0]) { // +Infinity -Infinity 统一k = null;b = (double) points[i][0];}String sk = String.format("%.7f", k); // string kif (!kMap.containsKey(sk)) {kMap.put(sk, new HashMap<>());}String sb = String.format("%.7f", b); // string bMap<String, Integer> bMap = kMap.get(sk);if (!bMap.containsKey(sb)) {bMap.put(sb, 0);}int nb = bMap.get(sb) + 1; // new bbMap.put(sb, nb);max = Math.max(max, nb);}}return (int) ((1 + Math.sqrt(1 + 8 * max)) / 2);}
}

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

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

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

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

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

  4. LeetCode 149 直线上最多的点数

    一.题目介绍 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o | ...

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

  6. Leetcode刷题149. 直线上最多的点数

    给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o |     o | ...

  7. 直线上最多的点数 | leetcode 149

    题目 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/max-point ...

  8. leetcode 149. 直线上最多的点数

    题目 给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点.求最多有多少个点在同一条直线上. 示例 1: 输入:points = [[1,1],[ ...

  9. 149. 直线上最多的点数

    给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点.求最多有多少个点在同一条直线上. 示例 1: 输入:points = [[1,1],[2,2 ...

最新文章

  1. 图像低频高频区域分离 小波变换
  2. SAP Netweaver和Cloud Foundry上的运行时进程
  3. window10使用 Hyper-V 创建ubuntu虚拟机并使用增强会话功能
  4. linux挂载多个目录下,linux – 如何在centos上使用nfs4挂载多个文件夹?
  5. r3 4300u r5 u_R5刀粒
  6. 山东自考c语言程序设计停考了吗,山东自考教育类停考专业遗留问题的通知
  7. [转载]linux下上传文件真的需要ftp么?
  8. Spring中实现自定义事件
  9. matlab车流量检测,求解释代码,一段交通车流量检测的代码
  10. 汽车诊断协议UDS概述
  11. npm切换到国内华为云的镜像
  12. 电力用高压锅炉管物理性能检验
  13. 办公office 2019软件有哪些
  14. Java:判断是一年的第几天
  15. Vue:把组件作为自定义元素来使用以及Identifier ' simple_couter' is not in camel case问题解决
  16. [转载]jQuery诞生记-原理与机制
  17. python_matplot_图片批量保存
  18. Redis - 使用scan代替keys与hgetall操作
  19. 解决Access denied for user ''@'localhost' to database 'mysql
  20. 计算机设备资产台帐,固定资产登记台帐.doc

热门文章

  1. POJ - 2115 C Looooops(扩展欧几里得)
  2. visio 形状_分享我Visio经验(问题从来都不是问题)
  3. 两条信号之间加电容_上海贴片电阻电容厂家地址-华腾电子
  4. 两个摄像头自动切换画面_利用OBS虚拟摄像头实现假装开会
  5. 斐波那契数拓展问题:leetcode-70 爬楼梯问题 leetcode-1137 泰波那契数问题解法
  6. SQL 学习最强刷题网站!
  7. SQL 性能优化梳理 —— 基本概念、创建时优化、查询时优化
  8. 数据库、缓存、消息队列之外的下一站——Elasticsearch
  9. 音视频技术开发周刊 | 158
  10. 音视频技术开发周刊 75期