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

【题意】

求二维平面上n个点中,最多共线的点数。

【思路】

比较直观的方法是,三层循环,以任意两点划线,判断第三个点是否在这条直线上。

【Java代码】

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
    public int maxPoints(Point[] points) {

        if (points.length < 3) return points.length;//点数少于3,直接输出

        int max = 0;//用于保存最终结果,即共线点的最大个数

        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < points.length; j++) {//固定两个点
            	if (i == j) continue;
                int cnt = 2;//用于保存跟这两个点斜率相同的点的个数(包括这两个点)
                int ycnt = 2;//用于保存y值相同的点的个数,本代码计算斜率时误用成了 (x1-x2)/(y1-y2)

                if (points[i].y == points[j].y) {//如果这两个点y值相同,不用能用(x1-x2)/(y1-y2)计算斜率
                    for (int k = 0; k < points.length; k++) {
                        if (k == i || k == j) continue;
                        if (points[k].y == points[i].y) ycnt++;
                    }
                } else {//如果这两个点y值不同,可以用(x1-x2)/(y1-y2)计算斜率
                    double k1 = (double)(points[j].x - points[i].x) / (points[j].y - points[i].y);//固定的两个点的斜率
                    for (int k = 0; k < points.length; k++) {
                        if (k == i || k == j) continue;
                        double k2 = (double)(points[k].x - points[i].x) / (points[k].y - points[i].y);
                        double k3 = (double)(points[k].x - points[j].x) / (points[k].y - points[j].y);
                        if (k1 == k2 || k1 == k3) cnt++;//因为第三个点可能与固定的两个点中的某个重合,所以要分别计算这个点与原来两个点的斜率。重合时,斜率就会为 0.0/0.0=NaN。幸亏是double类型,如果是 0/0 就会报错
                    }
                }
                if (cnt > max) max = cnt;//如果当前解大于最优解,则替换原始最优解
                if (ycnt > max) max = ycnt;
            }
        }
        return max;
    }
}

没想到这样的代码居然能通过!斜率k1是原来两点的斜率,k2,k3分别是第三个点与原来两个点的斜率,这样做是考虑第三个点与原来两个点重合的情况。但是又想,如果原来两点为{1, 1}和{2, 2},第三个点为{1, 1}, 那k2岂等于0/0?可是程序并没有报错。后来在eclipse里测试了一下 System.out.println(0 / 0),会抛异常;但是 System.out.println(0.0 / 0.0),却不会抛异常,输出
NaN,百度之——

NaN,是Not a Number的缩写,用于处理计算中出现的错误情况,比如 0.0 除以 0.0 或者求负数的平方根。

因为我在计算k2和k3时,是转换成double类型计算的,所有不会报错。但这算是侥幸通过吧,最好改成下面酱紫:

                        if (points[k].y - points[i].y != 0) {//如果第三个点与固定的这个点y值不同,<span style="font-family: Arial, Helvetica, sans-serif;">可以用(x1-x2)/(y1-y2)计算斜率</span>
                            k2 = (double)(points[k].x - points[i].x) / (points[k].y - points[i].y);
                        } else {
                            k2 = (double)(points[k].x - points[j].x) / (points[k].y - points[j].y);
                        }
                        if (k1 == k2) cnt++;

【易错点】

1. 只考虑横坐标相等或纵坐标相等的情况,忽略斜率相等的情况,如{{0, 0}, {1, 1}, {-1, -1}};

2. 忽略两点重合的情况,如{{1,, 1}, {1, 1}, {2, 2}, {2, 2}};

3. 忽略只有一个点和两个点的情况。

时间: 2024-08-24 10:27:40

【LeetCode】Max Points on a Line的相关文章

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma

【leetcode】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. 思路: 自己脑子当机了,总是想着斜率和截距都要相同.但实际上三个点是一条直线的话只要它们的斜率相同就可以了,因为用了相同的参照点,截距一定是相同的. 大神的做法: 对每一个点a, 找出所有其他点跟a的连线斜率,相同为同一条线,记录下通过a的点的线上最大的点数. 找出每一个点的最大连线通过的点数. 其

【leetcode刷题笔记】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. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

[LeetCode OJ] 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.

//定义二维平面上的点struct Point { int x; int y; Point(int a=0, int b=0):x(a),y(b){} }; bool operator==(const Point& left, const Point& right) { return (left.x==right.x && left.y==right.y); } //求两个点连接成的直线所对应的斜率 double line_equation(const Point&

LeetCode OJ - 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. 解题思路: 第一反应:枚举两个点组成的直线,然后看其他的点在不在这条直线上,在此过程中统计最大值.此思路的复杂度 O(n^3) 参考了网上的思路:枚举第一个点,用unordered_map来记录其余的点和这个点的斜率,若斜率相同则代表这些点在同一直线上.避免double问题,把斜率转化成化简

[LeetCode][JavaScript]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 same straight line. 求有多少点在一直线上. 粗暴地用二重循环遍历. 每一轮都构造一个哈希表,用来记录斜率,斜率k = (y1 - y2) / (x1 - x2). 注意到特殊情况: 1.两点重合,用countSamePoint记下重复的点,最后加到结果上. 2.两点与X轴平行,

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 same straight line. 这道题需要稍微转变一下思路,用斜率来实现,试想找在同一条直线上的点,怎么判断在一条直线上,唯一的方式也只有斜率可以完成,我开始没想到,后来看网友的思路才想到的,下面是简单的实现:其中有一点小技巧,利用map<double, int>来存储具有相同斜率

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. 解题思路: 本题主要需要考虑到斜线的情况,可以分别计算出过points[i]直线最多含几个点,然后算出最大即可,由于计算points[i]的时候,前面的点都计算过了,所以不需要把前面的点考虑进去,所以问题可以转化为过points[i]的直线最大点的个数,解题思路是用一个HashMap储存斜率,遍历p

[Leetcode][JAVA] 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. 对每个点,考察其他点与它组成的直线斜率,使用HashMap将斜率与点个数对应起来. 需要注意的一点是特殊斜率,比如两点重复,两点横坐标相同.所以用String表示斜率比较好 获取斜率函数: 1 public String getSlope(Point p1, Point p2) 2 { 3 if(p