题目

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. 网页图表Highcharts实践教程标之添加题副标题版权信息

    网页图表Highcharts实践教程标之添加题副标题版权信息 Highcharts辅助元素 辅助元素图表的非必要元素,如标题.版权信息.标签.载入动态.它们不和图表数据发生关联,只是额外说明一些基本信 ...

  2. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem C. Cargo Transportation 暴力

    Problem C. Cargo Transportation 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed ...

  3. URAL 1963 Kite 计算几何

    Kite 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/C Description Vova bought a kite con ...

  4. 有强大的cURL,忘掉httpclient的吧!

    这段时间想做一个网页采集的程序,由于一网站采用了防采集的办法,我的httpclient总是在登录后无法获取到我想要过去的链接.在无数次的跟踪过后发现原来人家给返回的是javascript拼成的页面,而 ...

  5. Azure存储上传下载(断点续传)

    最近有一个客户需要将文件系统(VM搭建)迁移到Azure存储上,对于Azure存储这里就不多做介绍,但是该客户由于网络原因下载文件的时候经常出现上传中断,所以想在Azure 存储上实现下载的断点续传. ...

  6. 微信小程序自定义音频组件,自定义滚动条,单曲循环,循环播放

    小程序自定义音频组件,带滚动条 摘要:首先自定义音频组件,是因为产品有这样的需求,需要如下样式的 而微信小程序API给我们提供的就是这样的 而且产品需要小程序有后台播放功能,所以我们不考虑小程序的 a ...

  7. Redis-audit工具使用(转)

    在我的线上环境中,由于应用上对redis数据没有做冷热处理,所以经常会出现redis内存使用率居高不下的情况,一直以来都想知道都是什么样的数据比较消耗redis内存,就好比写一个sql语句放在数据库中 ...

  8. 导出文件在IE和火狐中文件名乱码问题的解决

    $ua = $_SERVER["HTTP_USER_AGENT"]; $filename = "客户数据.xls"; $encoded_filename = u ...

  9. Creating Reusable XAML User Controls with Xamarin Forms

    In the previous post on making fancy layouts with Xamarin Forms we saw how you can design a Dashboar ...

  10. 基於tiny4412的Linux內核移植 --- 实例学习中断背后的知识(2)

    作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台 tiny4412 ADK Linux-4.9 概述 前面一篇博文基於tiny4412的Linux內核移 ...