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. 关于Java的String字符串常量的长度问题

    虽然这种问题应该很难遇到,但是遇到了也会感到莫名其妙.不知道大家有没有遇到那种在java代码里用字符串写sql语句的情况,但是如果sql语句字符串的长度太长的话就会报错. 代码如下: 代码A Stri ...

  2. VUE获取焦点

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 正文内容 python3编码问题

    来源:http://www.jb51.net/article/92006.htm 以下是全文: 这两天写了个监测网页的爬虫,作用是跟踪一个网页的变化,但运行了一晚出现了一个问题....希望大家不吝赐教 ...

  4. 注册网站 captcha reCHAPTCHA 错误

    原因 出现这个错误,是因为注册和提交时候,没有正确输出验证码导致的.网站可能会为了防止恶意注册,而使用验证码.如果验证码没有被正确加载或验证,就会出现相关错误. 解决方案 如果是访问类似kaggle, ...

  5. Linux-异步IO

    1.何为异步IO (1).几乎可以这么认为:异步IO就是操作系统用软件实现的一套中断响应系统. (2).异步IO的工作方法:我们当前进程注册一个异步IO事件(使用signal注册一个信号SIGIO的处 ...

  6. springboot和shiro的整合

    直接贴上代码 1. 所需要的jar包 <dependency> <groupId>org.apache.shiro</groupId> <artifactId ...

  7. 基于Token的身份验证

    最近了解下基于 Token 的身份验证,跟大伙分享下.很多大型网站也都在用,比如 Facebook,Twitter,Google+,Github 等等,比起传统的身份验证方法,Token 扩展性更强, ...

  8. RabbitMQ 整合 SpringCloud实战

    RabbitMQ 整合 SpringCloud实战RabbitMQ 整合 SpringCloud实战rabbitmq-common 子项目rabbitmq-springcloud-consumer 子 ...

  9. centos 从头部署java环境

    1.首先安装lrzsz 上传下载服务 yum install -y lrzsz 2.然后检查是否已经安装java rpm -qa|grep java 如果已经安装卸载后再重新安装 3.将下载好的jdk ...

  10. 非线性支持向量机SVM

    非线性支持向量机SVM 对于线性不可分的数据集, 我们引入了核(参考:核方法·核技巧·核函数) 线性支持向量机的算法如下: 将线性支持向量机转换成非线性支持向量机只需要将变为核函数即可: 非线性支持向 ...