题目

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. 什么情况下,会用到fiddler或者charles?

    有的页面,比如设限制的html页面,比如原生页面,只能在手机APP里面查看,无法在电脑浏览器中打开查看,这时候,需要用fiddler抓包来查看返回数据,定位问题.

  2. QThreadPool线程池的开发使用

    QThreadPool+QRunnable线程池与QThread线程两种方式使用的场景不同,QThreadPool+QRunnable线程池主要用于那种不需要一直运行的任务,而QThread主要用于长 ...

  3. oracle like 条件拼接

    (1) ibatis xml配置:下面的写法只是简单的转义 namelike '%$name$%' (2) 这时会导致sql注入问题,比如参数name传进一个单引号“'”,生成的sql语句会是:nam ...

  4. Unity3d学习笔记记录

    1.发布到 ipad字体显示不出来,改变Position位置的Z轴为-1 2.发布打包有问题,记得用户权限有没有设置 3.ipad4分辨率:2048*1536 4.调整界面大小,尽量调整底下子对象位置 ...

  5. zoj 1649

    #include <iostream> #include <queue> using namespace std; int n,m,s2,e2; int b[205][205] ...

  6. PHP开启curl_init

    windows主机出现“Call to undefined function curl_init”错误提示,没有定义的函数,也就是php还没打开对curl_init函数的支持. 全文:http://j ...

  7. [Office Web Apps]实现在线office文档预览

    摘要 在使用office web apps实现office文档在线预览的时候,需要注意的地方. web api web api作为owa在线预览服务回调的接口,这里面核心代码片段如下: using H ...

  8. 内存映射函数remap_pfn_range学习——示例分析(1)

    span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror ...

  9. 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片

    在"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传"一文中,使用JSAjaxFileUploader这款插件实现了单文 ...

  10. future封装了callable,thread封装future。

    三.使用Callable,Future返回结果 总结:future封装了callable,thread封装future.将callable的返回结果封装在future中,thread封装future, ...