149. 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.

/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */
class Solution {
public:
    int maxPoints(vector<Point>& points) {
        int n = points.size(), i, j, ans = 0;
        if(1 == n)
            return 1;
        for(i = 0; i < n; i++)
        {
            map<double, int> m;
            map<int, int> x, y;
            int samePoint = 0;
            for(j = 0; j < n; j++)
            {
                int deltaX = points[i].x - points[j].x;
                int deltaY = points[i].y - points[j].y;
                if(0 == deltaX && 0 == deltaY)
                {
                    samePoint++;
                }
                else if(0 == deltaX)
                {
                    if(x.find(points[i].x) == x.end())
                        x[points[i].x] = 1;
                    else
                        x[points[i].x]++;
                }
                else if(0 == deltaY)
                {
                    if(y.find(points[i].y) == y.end())
                        y[points[i].y] = 1;
                    else
                        y[points[i].y]++;
                }
                else
                {
                    double t = 1.0 * deltaX / deltaY;
                    if(m.find(t) == m.end())
                        m[t] = 1;
                    else
                        m[t]++;
                }
            }
            ans = max(ans, samePoint);
            for(map<double, int>::iterator it = m.begin(); it != m.end(); it++)
                ans = max(ans, it->second+samePoint);
            for(map<int, int>::iterator it = x.begin(); it != x.end(); it++)
                ans = max(ans, it->second+samePoint);
            for(map<int, int>::iterator it = y.begin(); it != y.end(); it++)
                ans = max(ans, it->second+samePoint);
        }
        return ans;
    }
};
时间: 2024-10-04 02:50:25

149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数的相关文章

【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. 解析:题目要求是给定n个2维的点,求出总共有多少个点在同一条直线上.由数学知识可知,给定三个点a,b,c,如果三个点在一条直线上,则a和b的斜率与c和d的斜率是相同的.用哈希表来做,针对每个点,求出其余所有点与它的斜率,找出最大值.然后遍历所有的点,返回最大值. 特别需要主意的几点:1)可能存

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. 要求是输入n个坐标点,输出最多有多少个点在同一条直线上. 代码提交如下: 1 int Solution::maxPoints(vector<Point>& points) 2 { 3 if (points.size()<=2) 4 { 5 return points.size();

149 Max Points on a Line 直线上最多的点数

给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solution {

【leetcode】149 Max Points on a Line

题目说明 https://leetcode-cn.com/problems/max-points-on-a-line/description/ 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 解法1 首先遍历点,在第二层循环中使用map记录倾角斜率及其匹配次数. 斜率dx/dy(dx表示两点x轴的距离,dy表示两点y轴的差值)进行计算,但是这里有一种特殊情况,就是dy=0的情况,此种情况下点也在同一直线下,所以需要单独做判断. 还有一种情况就是如果点坐标一致,则不管其他点

[leedcode 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. /** * 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 clas

149. Max Points on a Line (Array; Greedy)

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() : x(0), y(0) {} Point(int a, int b)

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[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. /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solutio

No.149 Max Point on a Line

No.149 Max Point on a Line Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 法一:考虑不周,一条直线上的点会重复计数,但没找到解决方法[wrong] Input: [[0,0],[-1,-1],[2,2]]Output: 4Expected: 3 1 /** 2 * Definition for a point. 3 *