题目

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

题解

这道题就是给你一个2D平面,然后给你的数据结构是由横纵坐标表示的点,然后看哪条直线上的点最多。

(1)两点确定一条直线

(2)斜率相同的点落在一条直线上

(3)坐标相同的两个不同的点 算作2个点

利用HashMap,Key值存斜率,Value存此斜率下的点的个数。同时考虑特殊情况,如果恰巧遍历到一个相同坐标的点,那么就维护一个local的counter来记录相同的点。

维护一个localmax,计算当前情况下的最大值;再维护一个全局Max来计算总的最大值。

返回全局Max即可。

代码如下:

 1     public int maxPoints(Point[] points) {  

 2         if(points.length == 0||points == null) 

 3             return 0;  

 4             

 5         if(points.length == 1) 

 6             return 1;  

 7             

 8         int max = 1;  //the final max value, at least one

 9         for(int i = 0; i < points.length; i++) {  

             HashMap<Float, Integer> hm = new HashMap<Float, Integer>();  

             int same = 0;

             int localmax = 1; //the max value of current slope, at least one

             for(int j = 0; j < points.length; j++) {  

                 if(i == j) 

                     continue;  

                     

                 if(points[i].x == points[j].x && points[i].y == points[j].y){

                     same++; 

                     continue;

                 }

                 

                 float slope = ((float)(points[i].y - points[j].y))/(points[i].x - points[j].x); 

                 

                 if(hm.containsKey(slope))  

                     hm.put(slope, hm.get(slope) + 1);  

                 else  

                     hm.put(slope, 2);  //two points form a line

             }

             

             for (Integer value : hm.values())   

                 localmax = Math.max(localmax, value);  

           

             localmax += same;  

             max = Math.max(max, localmax);  

         }  

         return max; 

     }

Reference:

http://blog.csdn.net/ttgump/article/details/23146357

http://blog.csdn.net/linhuanmars/article/details/21060933

Max Points on a Line leetcode java的更多相关文章

  1. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

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

  3. 【leetcode】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 ...

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

  5. [LintCode] 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. ...

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

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

  8. 【Max Points on a Line 】cpp

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

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

随机推荐

  1. android listview 优化

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha android convertview作用 ======= 1,convertView复 ...

  2. hdu 4605 树状数组 ****

    题目大意很简单. 有一颗树(10^5结点),所有结点要么没有子结点,要么有两个子结点.然后每个结点都有一个重量值,根结点是1 然后有一个球,从结点1开始往子孙结点走. 每碰到一个结点,有三种情况 如果 ...

  3. Git和Gitlab

    参考 http://www.cnblogs.com/clsn/p/7929958.html#auto_id_16https://backlog.com/git-tutorial/cn/intro/in ...

  4. MikroTik RouterOS安装后初始化配置(PPPOE拨号上网)

    1.修改登入密码 路由器默认登入账号为admin,密码为空,强烈建议修改登入密码保证安全: 2.修改接口名称 选择Interface,切换到Ethernet标签,找到状态是R(run)的两个端口. 给 ...

  5. AspNetPager 控件使用

    使用方法: 1.添加对AspNetPager.dll的引用 2.在页面上拖放控件 3. <%@ Register assembly="AspNetPager" namespa ...

  6. HDU 4122 Alice's mooncake shop (RMQ)

    Alice's mooncake shop Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. ACE_Message_Queue和spawn实现(生产者/消费者)(V2.00)

    參考这里用到了线程管理.參考:http://blog.csdn.net/calmreason/article/details/36399697 以下的两个线程共享一个消息队列,一个用来放整数到队列,一 ...

  8. delphi TOnFormVisibleChangeEvent 事件应用

    TGQIFileMgrForm = class(TForm) 定义 property OnVisibleChange: TOnFormVisibleChangeEvent read FOnVisibl ...

  9. 使用IProgress实现异步编程的进程通知

    在异步编程中,有时候希望把进度展示出来,借助IProgress<in T>可以实现. IProgress<in T>只提供了一个方法void Report(T value),通 ...

  10. RabbitMQ的应用场景以及基本原理介绍(转)

    本文转自https://blog.csdn.net/whoamiyang/article/details/54954780 1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanve ...