149 Max Points on a Line

149 Max Points on a Line

链接:https://leetcode.com/problems/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.

Hide Tags Hash Table Math

这个问题就是求给出一些点的坐标,求在同一条直线上最多有几个点。直线表达式为y=kx+b,简单的解法就是遍历,取一个点p1,求出和其他点的斜率,如果斜率相同,那么这些点一定在同一直线上。需要注意的是斜率可能不存在情况。还有可能有和p1相同的点。自己用vector记录k和同线的点的数目不仅写代码比较麻烦,还不容易理解。

 struct data
 {
     double k;
     int numofpoint;
 };

class Solution {
public:
    int maxPoints(vector<Point>& points) {
        if(points.size()<3)
            return points.size();
        int result=0,numofsamepoint=0;
        vector<data> m;
        data temp;

        for(int i=0;i<points.size();i++)
        {
            for(int j=i+1;j<points.size();j++)
            {
                if(points[i].x==points[j].x&&points[i].y==points[j].y)
                {   
                    numofsamepoint++;
                    continue;
                }
                else if(points[i].x==points[j].x)
                {
                    temp.k=INT_MAX;
                    temp.numofpoint=2;
                    if(m.size()==0)
                     m.push_back(temp);
                    else
                    for(int k=0;k<m.size();k++)
                    {
                        if(m[k].k==INT_MAX)
                        {
                         m[k].numofpoint++;
                         break;
                        }
                        if(k==m.size()-1)
                       {
                            m.push_back(temp);
                            break;
                       }
                    }
                }
                else
                {
                   temp.k=double(points[i].y-points[j].y)/double(points[i].x-points[j].x);  
                   temp.numofpoint=2;
                   if(m.size()==0)
                       m.push_back(temp);
                   else
                   for(int k=0;k<m.size();k++)
                   {
                       if(m[k].k==temp.k)
                       {
                           m[k].numofpoint++;
                           break;
                       }
                       if(k==m.size()-1)
                       {
                            m.push_back(temp);
                            break;
                       }
                   }
                }   
            }
            result= result>numofsamepoint+1?result:numofsamepoint+1;
            for(int k=0;k<m.size();k++)
                       if(m[k].numofpoint+numofsamepoint>result)
                      result=m[k].numofpoint+numofsamepoint;
                m.clear();
                numofsamepoint=0;
        }
    return result;
    }
};

用C++的unordered_map可以更好的解决这个问题。

class Solution {
public:
    int maxPoints(vector<Point>& points) {
        if(points.size()<3)
            return points.size();
        int result=0,numofsamepoint=1;
        unordered_map<float,int> m;


        for(int i=0;i<points.size();i++)
        {
            for(int j=i+1;j<points.size();j++)
            {
                if(points[i].x==points[j].x&&points[i].y==points[j].y)
                {   
                    numofsamepoint++;
                    continue;
                }
                else 
                   m[points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - 
                   points[i].y)/(points[j].x - points[i].x)]++;

            }
            result= result>numofsamepoint?result:numofsamepoint;
            unordered_map<float, int>::iterator it = m.begin();  
            for(; it != m.end(); it++)  
                if(it->second + numofsamepoint > result)  
                      result=it->second + numofsamepoint;
            m.clear();
            numofsamepoint=1;
        }
    return result;
    }
}; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值