直线上最多的点数 | leetcode 149
题目
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/max-points-on-a-line
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
就是很简单的存储斜率和截距,但因为会有各种特殊情况,所以提交了好多遍…… 最最最最重要的是:别用double存储,会丢失精度 ,用字符串!
下面代码是这位作者提供的。
class Solution {public int maxPoints(int[][] points) {if (points.length < 3) return points.length; // none\one\two pointsint maxCount = 1;for (int i = 0; i < points.length - 1; i++)maxCount = Math.max(computeMaxPointsThrougthIndexPoint(points, i), maxCount);return maxCount;}private int horizontalLines; // 与指定点i在同一水平线点数private Map<String, Integer> linePointsMap = new HashMap<>(); // 与指定点i在同一斜线点数private int count; // 指定点i在同一直线最大点数private int duplicates; // 与指定点i重复的点数public int computeMaxPointsThrougthIndexPoint(int[][] points, int i) {clearStatisticData();for (int j = i + 1; j < points.length; j++) statistic(points, i, j); return this.count + this.duplicates;}private void clearStatisticData() {this.linePointsMap.clear(); // 清空,重新统计this.horizontalLines = 1; // 水平线this.count = 1; // 当前在一条线上的点,初始化为1,自己this.duplicates = 0; // 重复点统计}private void statistic(int[][] points, int i, int j) {int x1 = points[i][0], y1 = points[i][1];int x2 = points[j][0], y2 = points[j][1];if ((x1 == x2) && (y1 == y2)) duplicates++; // 重复点统计else if (y1 == y2) { // 水平线this.horizontalLines += 1;this.count = Math.max(horizontalLines, this.count);}else { // 斜线统计String slope = buildSlope(x1 - x2, y1 - y2); this.linePointsMap.put(slope, this.linePointsMap.getOrDefault(slope, 1) + 1);this.count = Math.max(this.linePointsMap.get(slope), count);}}private String buildSlope(int p, int q) {int gcd = computeGcd(p, q);return p / gcd + "_" + q / gcd;}private int computeGcd(int p, int q) {if (q == 0) return p;int r = p % q;return computeGcd(q, r);}
}
直线上最多的点数 | leetcode 149相关推荐
- LeetCode 149 直线上最多的点数
一.题目介绍 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...
- Leetcode刷题149. 直线上最多的点数
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- leetcode 149. 直线上最多的点数
题目 给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点.求最多有多少个点在同一条直线上. 示例 1: 输入:points = [[1,1],[ ...
- 149. 直线上最多的点数
给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点.求最多有多少个点在同一条直线上. 示例 1: 输入:points = [[1,1],[2,2 ...
- 直线上最多的点数java
给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点.求最多有多少个点在同一条直线上. 示例 1: 输入:points = [[1,1],[2,2 ...
- LeetCode149. 直线上最多的点数: 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | o ±------- ...
- Leetcode--149. 直线上最多的点数
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- leetcode 149. Max Points on a Line |149. 直线上最多的点数(Java)
题目 https://leetcode.com/problems/max-points-on-a-line/ 题解 hard 题,普通解法不难,有几个小坑: key : key : value 的存储 ...
- 力扣 149. 直线上最多的点数
题目 给定一个数字,我们按照如下规则把它翻译为字符串:0 翻译成 "a" ,1 翻译成 "b",--,11 翻译成 "l",--,25 翻译 ...
最新文章
- VMWare12 安装 OSX 10.10
- floyd判环算法(龟兔赛跑算法)
- 项目属性--生成事件--后期生成事件命令行
- 利用Python读取外部数据文件
- 牛逼!java反射创建类实例的三种方式
- 云之幻哔哩哔哩uwp_【UWP】一款很 Fluent Design 的 Bilibili UWP客户端:哔哩
- java jdk 文档下载_Java帮助文档 JDK全系列 官方中英下载地址
- coreseek mysql安装_coreseek安装和使用(一)
- 数据仓库--数据分层(ETL、ODS、DW、APP、DIM)
- 通讯录管理系统(C++基础 汇总案例)
- html5 canvas 虚线,HTML5 canvas画带箭头的虚线
- 街头篮球手游服务器维护,街头篮球手游2017.6.22维护更新公告 宝箱位置调整更新一览...
- 设正整数n的十进制表示为n=ak……a1a0(0=ai=9,0=i=k,ak!=0),n的个位为起始数字的数字的正负交错之和T(n)=a0+a1+……+(-1)kak,证明:11|n的充分必要...
- java中catch什么意思_你真的理解Java中的try/catch/finally吗?
- 自建邮件服务器无法发附件,搭建好服务器后不能够发邮件也不能收邮件
- 工业相机及镜头的简单全面介绍
- Linux 命令(142)—— hexdump 命令
- coso2dx-lua 电脑模拟器 , 不重启游戏 直接让修改过的 lua 代码 生效
- iPhone的快捷键
- 语义分割评估指标MIOU