前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >149. 直线上最多的点数

149. 直线上最多的点数

作者头像
张伦聪zhangluncong
发布2022-10-26 18:09:39
1390
发布2022-10-26 18:09:39
举报

给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。

示例 1:

代码语言:javascript
复制
输入: [[1,1],[2,2],[3,3]]
输出: 3
解释:
^
|
|        o
|     o
|  o  
+------------->
0  1  2  3  4

示例 2:

代码语言:javascript
复制
输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
输出: 4
解释:
^
|
|  o
|     o        o
|        o
|  o        o
+------------------->
0  1  2  3  4  5  6

解:未AC,题目不难,两层for循环求斜率,对于一个固定点和另一点求得的斜率相同说明在一条直线,用例会出现重复的点,这个非常麻烦。

代码语言:javascript
复制
 class Point {
    int x;
    int y;

    Point() {
        x = 0;
        y = 0;
    }

    Point(int a, int b) {
        x = a;
        y = b;
    }

    @Override
    public int hashCode() {
        return 10;
    }

    @Override
    public boolean equals(Object obj) {
        Point p = (Point) obj;
        return this.x == p.x && this.y == p.y;
    }
}

class Solution {
  public int maxPoints(Point[] points) {
        if (points.length == 0) {
            return 0;
        }
        Set<Point> set = new HashSet<>(Arrays.asList(points));
        if (set.size() == 1) {
            return points.length;
        }
        int max = 0;
        for (int i = 0; i < points.length; i++) {
            Map<Double, Integer> map = new HashMap<>();
            int yCount = 0;
            int cf = 0;
            for (int j = i + 1; j < points.length; j++) {
                if (points[i].x != points[j].x) {//不重合不垂直
                    double k = getK(points[i], points[j]);
                    if (map.containsKey(k)) {
                        map.put(k, map.get(k) + 1);
                    } else {
                        map.put(k, 1);
                    }
                } else if (points[i].y == points[j].y) {//重合
                    cf++;
                } else {//垂直
                    yCount++;
                }
            }
            for (Double d : map.keySet()) {
                max = Math.max(max, map.get(d) + cf);
            }
            max = Math.max(max, yCount + cf);
        }
        return max + 1;
    }

    private double getK(Point a, Point b) {
        return (b.y - a.y) / (b.x - a.x);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-08-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档