/*
* A line is determined by two factors,say y=ax+b
*
* If two points(x1,y1) (x2,y2) are on the same line(Of course). * Consider the gap between two points. * We have (y2-y1)=a(x2-x1),a=(y2-y1)/(x2-x1) a is a rational, b is canceled since b is a constant * If a third point (x3,y3) are on the same line. So we must have y3=ax3+b * Thus,(y3-y1)/(x3-x1)=(y2-y1)/(x2-x1)=a * Since a is a rational, there exists y0 and x0, y0/x0=(y3-y1)/(x3-x1)=(y2-y1)/(x2-x1)=a * So we can use y0&x0 to track a line;
*/ public class Solution{
public int maxPoints(Point[] points) {
if (points==null) return 0;
if (points.length<=2) return points.length; Map<Integer,Map<Integer,Integer>> map = new HashMap<Integer,Map<Integer,Integer>>();
int result=0;
for (int i=0;i<points.length;i++){
map.clear();
int overlap=0,max=0;
for (int j=i+1;j<points.length;j++){
int x=points[j].x-points[i].x;
int y=points[j].y-points[i].y;
if (x==0&&y==0){
overlap++;
continue;
}
int gcd=generateGCD(x,y);
if (gcd!=0){
x/=gcd;
y/=gcd;
} 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{
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
m.put(y, 1);
map.put(x, m);
}
max=Math.max(max, map.get(x).get(y));
}
result=Math.max(result, max+overlap+1);
}
return result; }
private int generateGCD(int a,int b){ if (b==0) return a;
else return generateGCD(b,a%b); }
}

参考:https://leetcode.com/problems/max-points-on-a-line/discuss/47113/A-java-solution-with-notes

leetcode149的更多相关文章

  1. [Swift]LeetCode149. 直线上最多的点数 | 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. ...

  2. LeetCode149: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 ...

随机推荐

  1. Python selenium —— selenium与自动化测试成神之路

    From: https://blog.csdn.net/huilan_same/article/details/52559711 忽然想谈谈自动化的学习路径,因为发现很多人总是急于求成,不懂该如何学习 ...

  2. Lucene 4.0 正式版发布,亮点特性中文解读[转]

    http://blog.csdn.net/accesine960/article/details/8066877 2012年10月12日,Lucene 4.0正式发布了(点击这里下载最新版),这个版本 ...

  3. Git初级

    一,安装git 一键安装 Mac 或 Windows. 二,下载一个工具书 Git 命令手册 free Git cheat sheet 三,安装完成之后需要先配置两个基本配置:用户名和邮箱 $ git ...

  4. WAP用户评论简单实现瀑布流加载

    wap端经常会遇到产品或者评论的加载,但是分页的体验不是很好,所以选择通过js实现瀑布流加载方式. 第一步:需要动态加载的主要内容,这里需要动态加载的是li标签的内容 <ul class=&qu ...

  5. 廖雪峰Java2面向对象编程-4抽象类和接口-2接口

    1.接口的定义 抽象方法本质上是定义接口规范. 在抽象类中定义了一个抽象方法,子类中必须实现这个抽象方法. public abstract class Person{ public abstract ...

  6. idea+dubbo+zookeeper项目访问html页面的方法

    将js,html文件放入consumer的resource的static和template文件中 1 consumer的pom需要引入模板的jar包   <dependency>      ...

  7. hadoop集群部署

    1) 安装jdk 下载jdk-6u21-linux-i586.bin 然后修改/etc/profile: export JAVA_HOME=/usr/local/jdk export CLASSPAT ...

  8. 1121 Damn Single (25 分)

    1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who is bei ...

  9. CentOS 7安装Oracle 11gR2以及设置自启动(1)

    一.环境准备 1.正确无误的CentOS 7系统环境 虚拟机要求: 内存至少2G 处理器至少2个 根分区要大于20G(安装oracle很占空间,空闲空间要足够) 2.正确的JDK环境 CentOS 7 ...

  10. AAA及Radius

    一.AAA(Authentication.Authorization.Accounting) 验证.授权和记费 验证 Authentication :验证用户身份 授权 Authorization : ...