[抄题]:

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,次数))

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 有key必然有value,如果存在x的key就不用新建value了,不存在才要建
  2. 每个点的duplicate也是独立的,出现在i的循环中

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

最大公约数gcd,不等于0时才能除,位置要写对

public int getGcd(int a, int b) {
//recursive
if (b == 0) return a;
//position is important
else 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) {
//ini
int result = 0;
//how to rewrite?
HashMap<Integer, HashMap<Integer, Integer>> map = new HashMap<>(); //cc: less than 2 points
if (points == null) return 0;
if (points.length <= 2) return points.length; //count every point
for (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 slope
for (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 0
if (gcd != 0) {
x /= gcd;
y /= gcd;
}
//x stands for the difference
if (x == 0 && y == 0) {
duplicate++;
continue;
}
//map containsKey x or not, x contains y or not
if (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);
} //return
return result;
} public int getGcd(int a, int b) {
//recursive
if (b == 0) return a;
//position is important
else return getGcd(b, a % b);
}
}

149. Max Points on a Line同一条线上的最多点数的更多相关文章

  1. 【LeetCode】149. 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 on the ...

  2. leetcode 149. Max Points on a Line --------- java

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  3. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

  4. [LeetCode] 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. ...

  5. [leetcode]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. ...

  6. 149. 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. ...

  7. Java for LeetCode 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. ...

  8. 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 li ...

  9. [LC] 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. ...

随机推荐

  1. Go语言开发Windows应用

    Go语言开发Windows应用 当第一次看到Go程序在windows平台生成可执行的exe文件,就宣告了windows应用也一定是Go语言的战场.Go不是脚本语言,但却有着脚本语言的轻便简单的特性.相 ...

  2. java实现表格tr拖动

    实现功能:js实现表格tr拖动,并保存因为拖动改变的等级. jsp代码 <div id="mainContainer"> <div class="con ...

  3. python中的is和==

    is和== Python中的对象包含三要素:id.type.value id方法的返回值就是对象的内存地址其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值 is 判断的是a对 ...

  4. SQL注入检测

    目前只支持单个对象,不支持对象中包含对象 /// <summary> /// 检查SQL中的非法字符 /// </summary> public class SQLInject ...

  5. 如何系统学习知识图谱-15年+IT老兵的经验分享

    一.前言 就IT而言,胖子哥算是老兵,可以去猝死的年纪,按照IT江湖猿龄的规矩,也算是到了耳顺之年:而就人工智能而言,胖子哥还是新人,很老的新人,深度学习.语音识别.人脸识别,知识图谱,逐个的学习了一 ...

  6. 学Python的原因

    先立个旗,不学会誓不为人!!!!!!!!!!! 一直以来总是三天打鱼,两天晒网的学习,但是在体制内混久了发现,失去了很多的东西,得到的确极其有限,总感觉这样的生活会失去意义. 寻找生活的激情,重新发现 ...

  7. 在IntelliJ IDEA中使用VIM

    IdeaVim(下载)插件可以让你在IntelliJ IDEA中键盘敲的飞起. 安装 打开IDEA的设置,在Plugins里,你可以选择在线搜索Vim安装,当然如果不行,就可以选择单独下载后安装,以下 ...

  8. Python【每日一问】02

    问:列表 test = [1,2,3,1,3,4,5,67,7,8,54,1,2,3,4,5,6],如何删除该列表的重复元素? 方法1:利用集合的不重复性 # 利用集合的不重复性 test = [1, ...

  9. 20165312 2017-2018-2 《JAVA程序设计》第5周学习总结

    20165312 2017-2018-2 <JAVA程序设计>第5周学习总结 一.本周学习内容总结 总的来说,本周学习较吃力,在理解第十章的代码时速度较慢. 内部类 内部类是定义在一个类中 ...

  10. ssh免密钥之上厕所

    ssh服务简单介绍 SSH协议框架中最主要的部分是三个协议: *传输层协议(The Transport Layer Protocol)提供服务器认证,数据机密性,信息完整性等的支持; *用户认证协议( ...