题目

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. NLP文本相似度(TF-IDF)

    本篇博文是数据挖掘部分的首篇,思路主要是先聊聊相似度的理论部分,下一篇是代码实战.       我们在比较事物时,往往会用到“不同”,“一样”,“相似”等词语,这些词语背后都涉及到一个动作——双方的比 ...

  2. HDU.4035.Maze(期望DP)

    题目链接 (直接)设\(F(i)\)为在\(i\)点走出迷宫的期望步数.答案就是\(F(1)\). 令\(p_i=1-k_i-e_i\),表示\(i\)点沿着边走的概率:\(d_i=dgr[i]\), ...

  3. Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学

    C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...

  4. 使用 IntraWeb (22) - 基本控件之 TIWCalendar

    TIWCalendar: 日历控件, 继承于 TIWCustomGrid, 所以它和 TIWGrid 共同属性特多. 它的 Cell 是 TIWCalendarCell 对象, 直接从 TIWGrid ...

  5. [Go] 第一个单词首字母变大写:Ucfirst(),第一个单词首字母变小写:Lcfirst()

    import ( "unicode" ) func Ucfirst(str string) string { for i, v := range str { return stri ...

  6. Linux/CentOS服务器 一个网卡绑定多IP地址(永久设置)

    有时我们在使用 Linux 服务器时需要配置多个IP地址.如果要配置多个IP地址是否需要多块网卡呢?答案是否定的.以 CentOS 系统为例,多个 IP 地址是可以共享一块物理网卡的. 如何永久为单网 ...

  7. intel32指令中文版

    http://scc.qibebt.cas.cn/docs/optimization/VTune(TM)%20User's%20Guide/mergedProjects/analyzer_ec/mer ...

  8. Make the DbContext Ambient with UnitOfWorkScope(now named DbContextScope by mehdime)

    The Entity Framework DbContext (or LINQ-to-SQL DataContext) is a Unit Of Work implementation. That m ...

  9. 基于设备树的TQ2440 DMA学习(1)—— 芯片手册

    作者 彭东林pengdonglin137@163.com 平台 TQ2440内核Linux4.9 概述 一直想抽时间学习一下DMA驱动,今天就以S3C2440为例,这款芯片的DMA控制器足够简单,也比 ...

  10. 用Qemu搭建x86_64学习环境

    作者信息 作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 软件平台 主机: Ubuntu14.04 64位版本 模拟器:Qemu-2.8.0 Linux内核 ...