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

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...
  • fly_yr
  • fly_yr
  • 2015-11-09 13:24
  • 1289

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

LeetCode149—Max Points on a Line

leetcode149 Max Points On a Line

[LeetCode]149. Max Points on a Line 深入浅出讲解和代码示例

1、汇总概要 以下思路涵盖了哈希、数学计算等知识点 2、题目 Given n points on a 2D plane, find the maximum number of points th...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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