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. springBoot中的邮件发送

    1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. jq切换选择项

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 我读《DOOM启世录》——成为一个真正厉害的人

    序言 谈到游戏, 你的当然会想到几乎统治游戏市场多年的英雄联盟,你可能还会想起前段时间风头大盛的王者荣耀手游,你应该还会想起正在冲击着游戏市场的"吃鸡"类型游戏. 那么, 大家是否 ...

  4. HttpClient4.x 上传文件

    https://blog.csdn.net/wsdtq123/article/details/78888734

  5. IDA解析so文件异常(Binary data is incorrect maximum possible value is xx)

    错误信息 Binary data is incorrect maximum possible value is 0 错误原因 so文件损坏 或者ida换成32 解决办法 重新获得so文件,或者调整id ...

  6. java生成6位数所有组合

    for(int i=0;i<=9;i++){ String str=""; str=str+i; String strj=""; for(int j=0; ...

  7. [PHP防火墙]输入内容存在危险字符,安全起见,已被本站拦截

    之前在很多的网站都看到了360webscan的攻击拦截脚本,正好分析并学习一下. 下载地址:http ://webscan.360.cn/protect/down?domain = blog.dybo ...

  8. Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  9. dubbo使用Spring配置暴露服务和使用Spring配置引用远程服务

    提供者: <!-- 1.指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) --> <dubbo:application name="user-servic ...

  10. 六、Shell脚本高级编程实战第六部

    一.写一个start_nginx脚本,当启动.停止.重启时利用系统函数模拟实现系统脚本启动的特殊颜色效果 (用if实现) #!/bin/sh. /etc/init.d/functions if [ $ ...