【LeetCode】#149直线上最多的点数(Max Points on a Line)

【LeetCode】#149直线上最多的点数(Max Points on a Line)

题目描述

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

示例

示例 1:

输入: [[1,1],[2,2],[3,3]]
输出: 3
解释:

^
|
|        o
|     o
|  o  
+------------->
0  1  2  3  4

示例 2:

输入: [[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

Description

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

Example

Example 1:

Input: [[1,1],[2,2],[3,3]]
Output: 3
Explanation:

^
|
|        o
|     o
|  o  
+------------->
0  1  2  3  4

Example 2:

Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Explanation:

^
|
|  o
|     o        o
|        o
|  o        o
+------------------->
0  1  2  3  4  5  6

解法

class Solution {
    public int maxPoints(Point[] points) {
        if(points.length==0)
            return 0;
        int[] count = new int[points.length];
        for(int i=0; i<points.length; i++){
            count[i] = 1;
            int size = 1;
            int same = 0;
            HashMap<Integer[], Integer> hashMap = new HashMap<>();
            for(int j=0; j<points.length; j++){
                if(i!=j){
                    if(points[i].x != points[j].x){
                        int dy = points[i].y - points[j].y;
                        int dx = points[i].x - points[j].x;
                        int gcd = helper(dy, dx);
                        if(gcd!=0){
                            dy = dy/gcd;
                            dx = dx/gcd;
                        }
                        Integer[] nums = new Integer[2];
                        nums[0] = dy;
                        nums[1] = dx;
                        boolean flag = false;
                        for(Integer[] array : hashMap.keySet()){
                            if(nums[0]==array[0]&&nums[1]==array[1]){
                                flag = true;
                                hashMap.put(array, hashMap.get(array)+1);
                            }
                        }
                        if(!flag){
                            hashMap.put(nums, 1);
                        }
                    }else{
                        if(points[i].y==points[j].y){
                            same++;
                        }
                        size++;
                    }
                }
            }
            for(Integer[] array : hashMap.keySet()){
                if(hashMap.get(array)+1>count[i]){
                    count[i] = hashMap.get(array)+1;
                }
            }
            count[i] += same;
            count[i] = Math.max(count[i], size);
        }
        int maxIndex = 0;
        for(int i=1; i<count.length; i++){
            if(count[i]>count[maxIndex]){
                maxIndex = i;
            }
        }
        return count[maxIndex];
    }
    public int helper(int x, int y){
        if(y==0){
            return x;
        }
        return helper(y, x%y);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值