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

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

class Solution {
public int maxPoints(int[][] points) {
if (points == null || points.length == 0) {
return 0;
}
int count = 0;
for (int i = 0; i < points.length; i++) {
Map<String, Integer> map = new HashMap<>();
int samePoints = 0;
int sameX = 1;
for (int j = 0; j < points.length; j++) {
if (j != i) {
if (points[j][0] == points[i][0] && points[j][1] == points[i][1]) {
samePoints += 1;
}
if (points[j][0] == points[i][0]) {
sameX += 1;
continue;
}
int denominator = points[i][0] - points[j][0];
int numerator = points[i][1] - points[j][1];
int gcd = getGCD(numerator, denominator);
String hashStr = (numerator / gcd) + "/" + (denominator / gcd);
map.put(hashStr, map.getOrDefault(hashStr, 1) + 1);
count = Math.max(count, map.get(hashStr) + samePoints);
}
}
count = Math.max(count, sameX);
}
return count;
} private int getGCD(int a, int b) {
if (a == 0) {
return b;
}
return getGCD(b % a, a);
}
}

[LC] 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多点共线

    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同一条线上的最多点数

    [抄题]: Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

  4. 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. ...

  5. 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. ...

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

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

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

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

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

随机推荐

  1. ubuntu 18.04 安装 Redis-server

    Redis 安装 Redis是一款内存键值存储,以其灵活性,性能和广泛的语言支持而闻名.本教程将演示如何在Ubuntu 18.04服务器上安装和配置Redis.主要内容包括: 安装 Redis Red ...

  2. 吴裕雄--天生自然MySQL学习笔记:MySQL 序列使用

    MySQL 序列是一组整数:1, 2, 3, ...,由于一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现. 使用 AUTO_INCREMENT ...

  3. 2019杭电暑假多校训练 第六场 Snowy Smile HDU - 6638

    很多题解都是简单带过,所以打算自己写一篇,顺便也加深自己理解 前置知识:线段树.线段树维护最大字段和.二维坐标离散化 题解: 1.很容易想到我们需要枚举所有子矩阵来得到一个最大子矩阵,所以我们的任务是 ...

  4. 合并两个django的queryset

    有queryset:A和B 要合并它们,根据网上的答案,貌似是用itertools库的chain对象比较好,地址 c=chain(x,y)   但是当c用于分页的时候,就有问题,会报chain没有le ...

  5. C到C++转变简述

    从 C 到 C++ 语言的转变 1.平时使用还以 printf, scanf 为主 printf 和 scanf 与 cin cout 相比效率更快 2.头文件改成如下,C++对C语言兼容 #incl ...

  6. 一、早期(Early Stage)

    一.早期(Early Stage) 如果单纯从零基础开始,早期(Early Stage)应该是一到两个月(由于英语与中文差异比与其他语言大,中国同学至少两个月,但也不应过长.我们的经验是一般中国同学会 ...

  7. c#学习笔记03——委托和事件

    委托:一种引用类型,这种类型可以用来定义方法签名,从而使用委托实现将方法作为参数传递给其他方法.类似于C++中的函数之争,使用委托使程序员可以将方法引用封装在委托对象内. 定义和声明委托: deleg ...

  8. JavaSE--压缩

    package util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java ...

  9. protobuf使用遇到的坑

    在这里具体的使用我不写了,可以参考下面接个连接,我只记录自己遇到的问题. https://www.cnblogs.com/autyinjing/p/6495103.html(此博客很详细,不过最好不要 ...

  10. 题解-------[ZJOI2009]对称的正方形

    传送门 题目大意 找到所有的上下左右都相同的正方形. 思路:二分+二维Hash 这道题我们首先想到不能暴力判断一个正方形是否合法. 然后我们发现当一个正方形合法时,以这个正方形为中心且比它小的正方形也 ...