java 平面上在一条直线上最多的点数

    /**
     * 在坐标系的第一象限上有N个点,请问:最多有多少个点在一条直线上?
     */

    public static void testPoints() {
        Point[] points = new Point[8];
        points[0] = new Point(1, 1);
        points[1] = new Point(2, 2);
        points[2] = new Point(3, 3);
        points[3] = new Point(4, 4);
        points[4] = new Point(2, 3);
        points[5] = new Point(3, 1);
        points[6] = new Point(3, 2);
        points[7] = new Point(1, 2);
        HashMap<Point, Integer> map = new HashMap();
        //在坐标系上划出来,明显是最多4个点
        //k : 斜率
        for (int i = 0; i < points.length; i++) {
            Point current = points[i];
            HashMap<Float, Integer> mapKofPoint = new HashMap();
            int vertical = 0;//垂直的,无k
            for (int j = 0; j < points.length; j++) {
                Point next = points[j];

                if (current.x == next.x && current.y == next.y) {
                    //self
                } else if (current.x == next.x) {
                    //无k
                    vertical++;
                } else {
                    float newK = (1f * (next.y - current.y)) / (next.x - current.x);
                    if (mapKofPoint.containsKey(newK)) {
                        mapKofPoint.put(newK, mapKofPoint.get(newK) + 1);
                    } else {
                        mapKofPoint.put(newK, 1);
                    }
                }
            }
            int result = 0;
            Iterator<Map.Entry<Float, Integer>> entryIterator = mapKofPoint.entrySet().iterator();
            while (entryIterator.hasNext()) {
                Map.Entry<Float, Integer> entry = entryIterator.next();
                if (entry.getValue() > result) {
                    result = entry.getValue();
                }

            }
            //加1是因为需要算上自己这个点
            result = Math.max(result,vertical)+1;
            map.put(current, result);
        }
        Iterator<Map.Entry<Point, Integer>> entryIterator = map.entrySet().iterator();
        while (entryIterator.hasNext()) {
            Map.Entry<Point, Integer> entry = entryIterator.next();
            System.out.println("testPoints--key=" + entry.getKey() + "--num=" + entry.getValue());
        }
    }


12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(1, 2)--num=3
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(2, 3)--num=2
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(3, 2)--num=3
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(1, 1)--num=4
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(3, 1)--num=3
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(3, 3)--num=4
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(4, 4)--num=4
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(2, 2)--num=4





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个典型的计算几何问题,可以使用哈希表进行求解。具体做法如下: 1. 遍历所有的点,计算它们两两之间的斜率,并以斜率为键,以点的数量为值,将它们存储在哈希表中。 2. 对于每个点,我们需要计算它和其它点之间的斜率。当我们计算完当前点和其它所有点之间的斜率后,我们可以从斜率哈希表中找到斜率相同的点,并将它们的数量相加。 3. 在遍历所有点的过程中,我们需要找到斜率相同的点的数量的最大值。这个最大值就是同一条直线上的点的数量。 下面是 Java 代码实现: ```java public int maxPoints(int[][] points) { if (points == null || points.length == 0) { return 0; } if (points.length <= 2) { return points.length; } int maxCount = 0; for (int i = 0; i < points.length; i++) { Map<Double, Integer> slopeMap = new HashMap<>(); int samePointCount = 0; int verticalCount = 0; int curMaxCount = 1; for (int j = i + 1; j < points.length; j++) { int dx = points[i][0] - points[j][0]; int dy = points[i][1] - points[j][1]; if (dx == 0 && dy == 0) { samePointCount++; } else if (dx == 0) { verticalCount++; curMaxCount = Math.max(curMaxCount, verticalCount); } else { double slope = dy * 1.0 / dx; int count = slopeMap.getOrDefault(slope, 1) + 1; slopeMap.put(slope, count); curMaxCount = Math.max(curMaxCount, count); } } maxCount = Math.max(maxCount, curMaxCount + samePointCount); } return maxCount; } ``` 其中,`slopeMap` 是斜率哈希表,`samePointCount` 是和当前点重合的点的数量,`verticalCount` 是和当前点在一条直线上的点的数量,`curMaxCount` 是当前点为起点时,同一条直线上的点的数量。 时间复杂度为 $O(n^2)$,空间复杂度为 $O(n)$。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值