Hard-题目53:149. Max Points on a Line

转载 2016年05月31日 23:58:47

题目原文:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
题目大意:
给出2维坐标系内的n个点,求出最多有多少个点共线。
题目分析:
对每个点,使用一个hashmap记录<斜率,点数>,这样就知道与这个点共线的点最多有多少个,然后维护最大值即可。
有几个坑,第一斜率要用double而不是int,第二有重复的点,那么该点的hashmap的所有value都+1,第三有斜率不存在的情况,用一个特殊值记录。
源码:(language:java)

public class Solution {
    public int maxPoints(Point[] points) {
        if(points.length <= 0) return 0;
        if(points.length <= 2) return points.length;
        int result = 0;
        for(int i = 0; i < points.length; i++){
            HashMap<Double, Integer> hm = new HashMap<Double, Integer>();
            int samex = 1;
            int samep = 0;
            for(int j = 0; j < points.length; j++){
                if(j != i){
                    if((points[j].x == points[i].x) && (points[j].y == points[i].y)){
                        samep++;
                    }
                    if(points[j].x == points[i].x){
                        samex++;
                        continue;
                    }
                    double k = (double)(points[j].y - points[i].y) / (double)(points[j].x - points[i].x);
                    if(hm.containsKey(k)){
                        hm.put(k,hm.get(k) + 1);
                    }else{
                        hm.put(k, 2);
                    }
                    result = Math.max(result, hm.get(k) + samep);
                }
            }
            result = Math.max(result, samex);
        }
        return result;
    }
}

成绩:
46ms,17.98%,27ms,7.06%
cmershen的碎碎念:
粗看还以为是传说中的计算几何,其实也是比较水的一道题,但考虑用hashmap存储斜率的思路还是不太好想到的。

149、Max Points on a Line (Hard)

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

LeetCode 149. Max Points on a Line(直线上的点)

原题网址:https://leetcode.com/problems/max-points-on-a-line/ Given n points on a 2D plane, find the max...
  • jmspan
  • jmspan
  • 2016年05月26日 01:12
  • 361

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

leetcode 149:Max Points on a Line(java实现)细致分析

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

【LeetCode】149. Max Points on a Line - javascript

提交效率:192 ms ,55% 这一题,出现了 javascript 精度问题。最后的案例 [[0,0],[94911151,94911150],[94911152,94911151]] 是没法通...

[leetcode]149. Max Points on a Line@Java解题报告

https://leetcode.com/problems/max-points-on-a-line/description/ Given n points on a...

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

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

Leetcode 149. Max Points on a Line

题目大意给定二维坐标下的n个点,求其中共线的点的最大个数。思路分析两个不同的点可以确定一条直线,设这两个点的坐标分别为(x1,y1)和(x2,y2),则这条直线的方程式可以写成:(x-x1)(y1-y...

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....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目53:149. Max Points on a Line
举报原因:
原因补充:

(最多只允许输入30个字)