问题描述:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
原问题链接:https://leetcode.com/problems/max-points-on-a-line/
问题分析
给定一个平面上若干个点来说,要计算有多少个点在一条线上,我们需要选择每个节点作为起点,看它到所有其他的点能连成一条线的有多少个。对于每个点的情况,取它所能覆盖的最大点个数。
在具体的实现中,有若干种情况需要考虑。对于一个点来说,可能有其他的点和它是在同一个点上,这个时候我们需要一个元素来计算它重复出现的个数。另外,对于和某个点在一条线上的元素,可能是在水平的x轴或者y轴上。还有的就是其他可能的情况。那么要判断其他的情况,我们可以通过计算平面上两个点之间的斜率来统计。
这样,我们可以通过在每访问一个点的时候建立一个Map<Double, Integer>,map里key表示从该点到另外一个节点的斜率,value表示这个斜率下的元素个数。而对于在垂直线上的元素来说,它相当于斜率是无穷大,我们可以用Double.MAX_VALUE来表示。每次我们碰到一个元素,就计算它的斜率并加入到map中。
详细的代码实现如下:
/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y = b; } * } */ public class Solution { public int maxPoints(Point[] points) { if(points.length <= 1) return points.length; int max = 0; for(int i = 0; i < points.length; i++) { Map<Double, Integer> map = new HashMap<>(); int duplicate = 1; for(int j = 0; j < points.length; j++) { if(i == j) continue; if(points[i].x == points[j].x && points[i].y == points[j].y) duplicate++; else if(points[i].y == points[j].y) map.put(Double.MAX_VALUE, map.containsKey(Double.MAX_VALUE) ? map.get(Double.MAX_VALUE) + 1 : 1); else { double slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x); map.put(slope, map.containsKey(slope) ? map.get(slope) + 1 : 1); } } if(map.isEmpty()) max = Math.max(max, duplicate); else { for(double d : map.keySet()) max = Math.max(max, map.get(d) + duplicate); } } return max; } }
相关推荐
正确的姿势,学习的态度来刷 LeetCode:高效的代码、简洁的注释、精炼的总结。
leetcode 非官方顺序leetcode题解,主要代码为Python和C++。 leetcode 第1题: leetcode 第2题: leetcode 第3题: leetcode 第4题: leetcode 第5题: leetcode 第6题: leetcode 第7题: leetcode 第9题: ...
LeetCode::laptop:LeetCode解决方案
leetcode11 top 1. 位运算 LeetCode191 : 二进制位1的个数 LeetCode338 : 比特位运算 2. 字典树 LeetCode209 : 实现一个Trie结构 LeetCode79 : 单词搜索(判断单词是否出现在给定的网格中) LeetCode212 : 单词搜索II...
leetcode 答案 leetCode :keyboard:我的 Leetcode 解题答案
LeetCode 101:和你一起你轻松刷题(C++)
Leetcode:Leetcode提交
LeetCode 在LeetCode和其他编码平台上解决的问题的集合
lru缓存leetcode 力码 涵盖了 Geeks for Geeks 和 Leet Code 的各种问题。 LeetCode 1 : 二和 (46_Easy) LeetCode 2 : 两个数字相加 (96_Medium) LeetCode 3 : 无重复字符的最长子串 (214_Medium) LeetCode 4 : 两个...
:fire: Leetcode :fire: 实践使完美 :party_popper: 开玩笑的单元测试 :sparkles: 简单的代码 :artist_palette: 可读代码 入门指南 git clone https: //github.com/tangweikun/leetcode.git cd leetcode npm ...
leetcode:leetcode刷题
leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 $number 题号代表经典 LeetCode 题目,$number.$number 题号表示 LeetCode 面试题。 :receipt: 目录 统计 值 AC 的...
leetcode 分类 LeetCode :bouquet::bouquet::bouquet: 介绍 leetcode 题解,Issues 会记录 leetcode 解题之路,并使用 label 进行了分类。 目录 链表
max points on a line leetcode ISCAS15 - leetcode - week1 唐波 任杰 王建飞 殷康 张一鸣 ISCAS15 - leetcode - week2 曾靖 刘重瑞 沉雯婷 刘旭斌 王建飞 ISCAS15 - leetcode - week3 殷康 张一鸣 赵伟 任杰 唐波 ...
idea中leetcode插件Rust 中的 LeetCode 解决方案 怎么跑?...,所有解决方案代码都在leetcode::leetcode::editor::en并重用于leetcode 。 它有一个全局结构Solution ,所有解决方案条目都在其中实现。
Leetcode:LeetCode解题代码
LeetCode:LeetCode的代码
LeetCode:LeetCode的注释
什么水平能刷leetcode leetcode :woman_biking: I like to brush leetcode, it is a way of my pastime. I really enjoy it, I will always update it. :open_book: :raised_fist: 不间断刷题天数:1天 :elephant: ...
leetcode:LeetCode问题