149. Max Points on a Line

本文针对LeetCode中的149题MaxPointsOnALine进行了解析,提出使用long double类型存储斜率以减少精度损失的方法,并讨论了如何通过简化分子与分母来表示斜率,最终给出两种解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

leetcode中第149题:Max Points on a Line,关于斜率,忙活了好长时间,使用double类型来存储斜率会造成斜率精度损失,可以使用long double类型,目前是通过测试了,个人感觉最好使用最简化的分子与分母表示斜率。对与有相同的斜率并拥有共同点的直线是一条直线,求得直线上的所有点即可,找出数量最大的即为所求。使用hash表来对相同的斜率计数。


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

给定若干个点,求共线的最多点数

点共线,由y=ax+b可知,如果斜率相同且有共同点则两条直线共线,即两条直线上的点都在一条直线上;‘
1:如果从一个点开始(作为公共点),依次遍历其他所有点,只需斜率相同即可。
分析:
    1.如果两个点的斜率不存在 即a.x=b.x,斜率无穷大
    2.斜率存在  (b.y-a.y)/(b.x-a.x) 
    3.重复的点 
采用一个hash表来统计不同斜率的直线上的点数(斜率使用long double类型表示)
2:使用最简化的分子分母表示斜率
*/

#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
//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:
/*
Submission Result: Wrong Answer More Details 
Input:
[[0,0],[94911151,94911150],[94911152,94911151]]
Output:
3
Expected:
2
double 存在误差;
把double类型修改为long double 类型通过测试
*/
    int maxPoints(vector<Point>& points) {
        int res=0;
        for(int i=0;i<points.size();i++)//选取公共点
        {
            int samePoints=1;
            unordered_map<long double,int> m;
            for(int j=i+1;j<points.size();j++)
            {
                if(points[i].x==points[j].x && points[i].y==points[j].y)
                    samePoints++;
                else if(points[i].x==points[j].x)
                    m[INT_MAX]++;
                else
                {
                    long double slopes =(long double)(points[j].y - points[i].y)/(long double)(points[j].x -points[i].x);
                    m[slopes]++;
                }
            }
            int maxP=0;
            for(unordered_map<long double,int>::iterator it=m.begin();it!=m.end();it++)
                maxP=max(maxP,it->second);
            maxP+=samePoints;
            res=max(res,maxP);
        }
        return res;//6 ms
    }
    //避免double的误差,存储化简后的分子、分母
    int maxPoints2(vector<Point>& points) {
        int res=0;
        for(int i=0;i<points.size();i++)//选取公共点
        {
            int samePoints=1,vertical=0;
            map<pair<int,int>,int> m;
            for(int j=i+1;j<points.size();j++)
            {
                if(points[i].x==points[j].x && points[i].y==points[j].y)
                    samePoints++;
                else if(points[i].x==points[j].x)
                    vertical++;
                else
                {
                    int dx=points[j].x-points[i].x;
                    int dy=points[j].y-points[i].y;
                    int r=gcd(dx,dy);
                    m[{dx/r,dy/r}]++;
                }   
            }
            int maxP=0;
            for(map<pair<int,int>,int>::iterator it=m.begin();it!=m.end();it++)
                maxP=max(maxP,it->second);
            maxP+=samePoints;
            res=max(res,maxP);
            res=max(res,samePoints+vertical);
        }
        return res;//9 ms
    }
    int gcd(int a,int b)
    {
        return (b==0) ? a : gcd(b,a%b);
    }
};


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值