`

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.

Solution:

public int maxPoints(Point[] points) {
        if(points.length < 3) return points.length;
        int result = 2; // at least 2 points on a line
        Map<Double, Integer> map = new HashMap<>();
        for(Point p:points) {
            map.clear();
            int duplicate = 1; // [(1,1),(1,1),(2,2),(2,2)] -> 4
            map.put(Double.MIN_VALUE, 0); // for special case: [(1,1),(1,1),(1,1)], no slope will be put to map
            for(Point q:points) {
                if(p == q) continue;
                if(p.x == q.x && p.y == q.y) {
                    duplicate++;
                    continue;
                }
                double slope = (p.x == q.x) ? Double.MAX_VALUE : (p.y-q.y)/(double)(p.x-q.x);
                Integer cnt = map.get(slope);
                if(cnt == null) cnt = 0;
                map.put(slope, cnt+1);
            }
            for(Integer cnt:map.values()) {
                result = Math.max(result, cnt+duplicate);
            }
        }
        return result;
    }

 

重构了下代码:

public int maxPoints(Point[] points) {
    if(points == null) return 0;
    int maxNum = 0;
    Map<Double, Integer> map = new HashMap<>();
    for(Point p:points) {
        int dup = 1;
        map.clear();
        for(Point q:points) {
            if(p == q) continue;
            if(p.x == q.x && p.y == q.y) {
                dup++; continue;
            }
            double slope = slope(p, q);
            Integer cnt = map.get(slope);
            if(cnt == null) cnt = 0;
            map.put(slope, cnt+1);
        }
        // for special case: [(1,1),(1,1),(1,1)], no slope will be put to map
        if(map.size() == 0) return points.length; 
        for(int cnt:map.values()) {
            maxNum = Math.max(maxNum, cnt+dup);
        }
    }
    return maxNum;
}

private double slope(Point p1, Point p2) {
    if(p1.x == p2.x) return Double.MAX_VALUE;
    return (p2.y-p1.y)/((double)(p2.x-p1.x));
}

 

分享到:
评论

相关推荐

    js-leetcode题解之149-max-points-on-a-line.js

    javascript js_leetcode题解之149-max-points-on-a-line.js

    python-leetcode题解之149-Max-Points-on-a-Line.py

    python python_leetcode题解之149_Max_Points_on_a_Line.py

    dna匹配leetcode-leetcode:leetcode刷题

    dna匹配 leetcode leetcode刷题--C++ 哈希表 Longest Substring Without Repeating Characters ...Points on a Line 斜率 map, int&gt; Fraction to Recurring Decimal map long long 正负号 Repeated DNA S

    leetcodemaxpointsonaline-leetcode:leetcode

    max points on a line leetcode ISCAS15 - leetcode - week1 唐波 任杰 王建飞 殷康 张一鸣 ISCAS15 - leetcode - week2 曾靖 刘重瑞 沉雯婷 刘旭斌 王建飞 ISCAS15 - leetcode - week3 殷康 张一鸣 赵伟 任杰 唐波 ...

    LeetCode最全代码

    Up to date (2016-12-18), there are `447` Algorithms / `13` Database / `4` Shell / `4` Draft questions on [LeetCode Online Judge](https://leetcode.com/). The number of questions is increasing recently...

    LeetCode:LeetCode解决方案

    LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...

    leetcode-answer-and-analysis(part).zip_图形图像处理_Java_

    2. **Max points on a line** (平面上最多的共线点): 此题考察的是线性代数和几何知识,以及如何在二维平面上找到共线点。在Java中,可以使用数据结构如HashMap来存储斜率和点的数量,从而有效地计算最大共线点数。 ...

    Selective_Leetcode_solutions

    2. 题目149 - "Max Points on a Line" 这道题目要求找到平面上最多共线的点。解决方法通常涉及线性代数,如计算两点之间的斜率,并通过哈希表记录斜率及其出现的次数。在Java中,可以使用HashMap来存储斜率和对应的...

    leetcodemaxpointsonaline-leetcode:https://oj.leetcode.com/的解决方案

    在LeetCode平台上,题目"Max Points on a Line"是一道典型的几何与图论结合的问题,旨在测试编程者对数据结构和算法的理解。本题要求找到二维平面上最多共线的点的数量。这个问题的关键在于理解如何有效地计算每一对...

Global site tag (gtag.js) - Google Analytics