149. 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.
Example 1:
Input: [[1,1],[2,2],[3,3]] Output: 3 Explanation: ^ | | o | o | o +-------------> 0 1 2 3 4
Example 2:
Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] Output: 4 Explanation: ^ | | o | o o | o | o o +-------------------> 0 1 2 3 4 5 6
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
数组为空时,特意指出是0个点
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
分子分母同时约分掉gcd之后,用双重hashmap存储(x,(y,次数))
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 有key必然有value,如果存在x的key就不用新建value了,不存在才要建
- 每个点的duplicate也是独立的,出现在i的循环中
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O() Space complexity: O()
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
最大公约数gcd,不等于0时才能除,位置要写对
public int getGcd(int a, int b) {//recursiveif (b == 0) return a;//position is importantelse return getGcd(b, a % b);}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
/*** Definition for a point.* 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) {//iniint result = 0;//how to rewrite?HashMap<Integer, HashMap<Integer, Integer>> map = new HashMap<>();//cc: less than 2 pointsif (points == null) return 0;if (points.length <= 2) return points.length;//count every pointfor (int i = 0; i < points.length; i++) {int count = 0;int duplicate = 0;//clear previous data map.clear();//calculate x,y / gcd and the slope//cc : same slopefor (int j = i + 1; j < points.length; j++) {int x = points[i].x - points[j].x;int y = points[i].y - points[j].y;int gcd = getGcd(x, y);//gcd musn't be 0if (gcd != 0) {x /= gcd;y /= gcd;}//x stands for the differenceif (x == 0 && y == 0) {duplicate++;continue;}//map containsKey x or not, x contains y or notif (map.containsKey(x)) {if (map.get(x).containsKey(y)) {map.get(x).put(y, map.get(x).get(y) + 1);}else {map.get(x).put(y, 1);}}else {HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();m.put(y, 1);map.put(x, m);}count = Math.max(map.get(x).get(y), count);}result = Math.max(result, count + duplicate + 1);}//returnreturn result;}public int getGcd(int a, int b) {//recursiveif (b == 0) return a;//position is importantelse return getGcd(b, a % b);} }
View Code
转载于:https://www.cnblogs.com/immiao0319/p/9369030.html
149. Max Points on a Line同一条线上的最多点数相关推荐
- 【重要+细节】LeetCode 149. Max Points on a Line
LeetCode 149. Max Points on a Line Solution1: 参考花花酱:https://zxi.mytechroad.com/blog/geometry/leetcod ...
- leetcode 149. Max Points on a Line |149. 直线上最多的点数(Java)
题目 https://leetcode.com/problems/max-points-on-a-line/ 题解 hard 题,普通解法不难,有几个小坑: key : key : value 的存储 ...
- 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 ...
- [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 ...
- 【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 ...
- 【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. ...
- [LeetCode Online Judge]系列-求二维平面内在一条直线上的最大点数
2019独角兽企业重金招聘Python工程师标准>>> Max Points on a Line Given n points on a 2D plane, find the max ...
- 最多有多少个点在一条线上
最多有多少个点在一条线上 问题描述 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 样例1: 输入:(1,2),(3,6),(0,0),(1,3). 输出:3 样例2 输入:(1,2), ...
- workbench应力应变曲线_ansys workbench怎样输出一条线上的应力值
答:同学你好,关于ANSYS workbench中输出结构某一条线上的应力值,可以通过建立路径的方法来分析: 首先在ANSY workbench中导入模型后建立路径path,可以根据这条线上点的选取来 ...
最新文章
- 一个新手对linux的认识
- input中v-model和value不能同时调用时解决方案
- jquery 属性操作
- nginx php-fpm调优
- 【转】使用ArcGIS Engine开发ArcGIS Server的胖客户端浏览程序
- excel中空格去不掉java_在Apache POI中跳过空白Excel单元格
- java多线程编程_《java多线程编程实战指南》读书笔记 -- 基本概念
- sql增加字段默认为0_OUP2.0:mysql乐观锁不生效
- 尼康日本拟裁员约一千人 涉及半导体制造业务
- 修改文件属性与权限(鸟哥linux私房菜)
- android驱动程序失败,android studio 3.0中的haxm错误需要数字签名的驱动程序
- 中科大EPC抢预约考试脚本简单版
- 最全Android UVC Camera 闪退问题修复
- Frame-relay帧中继配置,实现网络连接
- 软件测试有效性指标,软件测试用例评审有效性的44个衡量标准[1]
- rsync命令排除文件和文件夹(exclude-from)
- ssm学生请假系统java学生请假系统源码
- Bootstrap——制作个人简历网页、工具类【边框(添加、删除、颜色、圆角)、清除浮动、颜色(文本、链接、背景)、display属性、浮动、定位、文本对齐】
- 中国石油大学华东2013-2014-1c语言a卷_答案,中国石油大学(华东)2012—2013学年第二学期期中A卷试卷答案...
- BCD码指令 AAA DAA AAS DAS AAM AAD
热门文章
- 搭建Ubuntu18.04+Anaconda3.x+Pycharm+SimpleITK(三)
- Python-turtle标准库知识小结(python绘图工具)
- 龙岗网络推广为SEO优化人员介绍如何合理处理垃圾外链?
- 网站导航目录要该如何优化?
- oracle dbms refresh,oracle 10g中dbms_mview.refresh()函数
- mysql 快照读 幻读,InnoDB的MVCC如何解决不可重复读和快照读的幻读,当前读用next-key解决幻读...
- 为什么图片要2的倍数_为什么电工作业时,至少要有2个人?电工保命四招要牢记!...
- 拳皇重生服务器维护,《拳皇97 OL》3月24日更新维护公告
- explode 无分隔符_使用PHP explode()函数时出现“空分隔符”警告
- Gitlab 生成 swagger 文档