Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
坐标轴上面有n个点,求共线的最大点数。
这道题是leetcode上通过率最低的,但是绝对不是最难的,几点共线我们可以根据y = k x + b算出:
1. 平行于y轴的垂线斜率设为最大值,平行于x轴的线与普通斜线没啥区别,不需要做特殊处理;
2. 比较棘手的是重复点
这道题,我重构了一个数据结构,包含了k,b从而确定了每一条线,二重遍历坐标轴中每一个点(时间复杂度为n*n)从而遍历了图中的每一条线,但是当遇到重合点的时候,不知道该如何下手了。
后来查到了这样的算法,压根就不需要关心斜率,对每个点进行雷达式的扫描处理,简直碉堡。
其算法思想是这样的:
1. 对每个点p进行循环扫描(扫描坐标中所有的点),并维护一个duplicate变量,初值为1,表示自己。
2. 扫描到点q时,计算直线pq的斜率k,这时b就不需要关心了,原因自己画图,想一下就不难知道。
2.1 当p点与q点重合时,duplicate++
2.2 计算斜率(边界case,斜率为∞),记录斜率为k的直线个数
3. 对坐标中所有的点处理完之后,遍历map中的value + duplicate的最大值,有一种特殊情况:所有的点都是重合点,map为空。
代码如下:
public int maxPoints(Point[] points) { if(points== null) return 0; if(points.length <= 2) return points.length; int max = 0; int duplicate = 1;//this field setting is amazing Map<Double,Integer> map = new HashMap<Double,Integer>(); for(int i = 0; i < points.length; i++){ map.clear(); duplicate = 1; Point p = points[i]; for(int j = 0 ; j < points.length; j++){ if(i == j) continue; Point tem = points[j]; double slope = 0.0; if(tem.x == p.x && tem.y == p.y){ duplicate ++; continue; }else if(tem.x == p.x){ slope = Integer.MAX_VALUE; }else{ slope = tem.y == p.y ? 0 : 1.0 * (tem.y - p.y) / (tem.x - p.x); } map.put(slope, map.containsKey(slope) ? map.get(slope) + 1 : 1 ); } if(map.keySet().size() == 0){ max = duplicate; } for(double key : map.keySet()){ max = Math.max(max, duplicate + map.get(key)); } } return max; }
相关推荐
max points on a line leetcode ISCAS15 - leetcode - week1 唐波 任杰 王建飞 殷康 张一鸣 ISCAS15 - leetcode - week2 曾靖 刘重瑞 沉雯婷 刘旭斌 王建飞 ISCAS15 - leetcode - week3 殷康 张一鸣 赵伟 任杰 唐波 ...
大佬的leetcode刷题笔记(c++版本)
Online-Judge 目录说明 utils:小工具包 Readme.py: A script that generates the README document. Spider.py: A spider that gets the problem id, problem name, and problem content of the PAT page and ...
leetcode 答案Online Judge 参考答案 简介 放置个人在线上解题系统所使用的程式码,仅供参考。 相关网站 网站 语系 中文+英文 中文 英文+中文 英文 英文 英文 英文 日文
leetcode 答案在线裁判练习 在线裁判的个人练习 目标 我从以下在线评委网站上获得的答案。 力码: 在编码器: 会津在线裁判:
leetcode算法。本书包含了 LeetCode Online Judge(http://leetcode.com/onlinejudge) 所有题目的答案,所有 代码经过精心编写,编码规范...全书的代码,使用 C++ 11 的编写,并在 LeetCode Online Judge 上测试通过。
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
LeetCode 刷题笔记
dna匹配 leetcode leetcode刷题--C++ 哈希表 Longest Substring Without Repeating Characters ...Points on a Line 斜率 map, int> Fraction to Recurring Decimal map long long 正负号 Repeated DNA S
leetcode 2 和 c 该存储库包含来自著名在线编码学习平台的解决方案,如 leetcode、coding bat、dcp 等。 力码 简单的 10 中等的 15 难的 1 编号 描述 网址 解决方案 1 给定一个字符串 s 和一个非空字符串 p,在 s 中...
Solution to LeetCode written by C++. All codes are tested to ACCEPTED on LeetCode online judge. Basic data structure and algorithm sample codes.
Online-Judge-Challenger 剑指OJ Latest Status: Weekly training goes on. 它是什么 一个记录和分享我在练习期间在线评委编码经验的存储库。 由于版权问题,我完成的 LeetCode 解决方案可能无法上传到此存储库。 ...
彩色版本 正版 pdf 精讲数据结构 + 算法 链表 树 图表 贪心算法 指针 动态规划 查找算法
leetcode中文版
leetcode 分类在线裁判 语言:C++和Java 形式:描述+解决方案+关键点(包括我的思考过程和需要注意的细节) 订单:根据我的心情 Tips:leetcode里面有分类。 希望大家都能找到理想的实习!
vs code LeetCode 插件
看leetcode吃力 Redline Red Line 红土大陆 Red Line,红土大陆,在海贼王中,伟大航路被红土大陆分成两部分,前半部分乐园和后半部分新世界。跨过红土大陆就是新世界。 本 Repo 是本人闲暇刷一些 leetcode 或者其他...