/*
* 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. ribbon的注解使用报错--No instances available for [IP]

    使用RestTemplate类调用其他系统的url的时候,加上ribbon的注解@LoadBalanced上这个注解之后访问,就报错了. 报错如下: 因为这里你不能直接访问地址,需要把地址改成你所调用 ...

  2. 【Graphite学习】系列学习文章-【转】

    Graphite 系列 #2:Carbon 和 Whisper GRAPHITE SERIES #1: PROVISION HARDWARE GRAPHITE SERIES #2: CARBON &a ...

  3. PAT 乙级 1063 计算谱半径(20) C++版

    1063. 计算谱半径(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 在数学中,矩阵的“谱半径”是指其特 ...

  4. 我的第一个react native

    虽然react native出来了很久,但是自己一直因为各种原因没有接触学习,中间尝试过一次,但是因为复杂的环境配置而放弃了.现在,终于因为公司的项目不得不去学习了,当然了,再配置开发环境上面,我还是 ...

  5. java1.8 新特性(关于 match,find reduce )操作

    match处理Integer集合 package lambda.stream; /** * @author 作者:cb * @version 创建时间:2019年1月4日 下午2:35:05 */ i ...

  6. C语言强化——链表(2)

    目录 链表的应用: 栈 循环队列 C语言实现动态数组 数组实现定长元素个数层次建树 队列实现不定元素个数层次建树 (*) 栈 栈(链表应用) "stack.h" #include ...

  7. Linux开机自动挂载windows网络共享

    yum install samba-client yum install cifs.utils yum install samba-common 命令: mount -v -t  cifs    // ...

  8. linux下新建(touch)\复制(cp)\剪切(mv)\删除(rm)文件

    touch :新建多个文件,中间用空格隔开 touch file1 file2 cp: mv:剪切 rm:删除文件

  9. ext.net tooltip

    业务场景:需要对grid表格中指定列显示tooltip. html: <form id="form1" runat="server"> <To ...

  10. 02-创建String对象

    创建一个String对象实在是太简单了,就是因为简单,所以有很多java程序员做了好几年的开发,也没有注意这些小细节问题 String字符串的本质就是char数据对象, 那么char[0]数组当中的一 ...