求直线被确定的最大次数

来源:互联网 发布:网络安装检测报告 编辑:程序博客网 时间:2024/05/02 01:20

问题描述:

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

在一个给定的n个点的平面,求在相同的直线最多的点的点数。

 

 

 

 

 

class Solution {

public:

    int maxPoints(vector<Point> &points){

        //k存在

        map<double,int> k0[points.size()];

        //k不存在

        int k1[points.size()] = {0};

        int k_maxnum = 0;

        vector<Point>::iterator it1 = points.begin();

        vector<Point>::iterator it_end = points.end();

        int position = 0;

        for(; it1 != it_end; it1++)

            {

            Point p1(it1->x,it1->y);

            for(vector<Point>::iterator it2 = points.begin();it2 != it_end;it2++)

                {

                //同个点

                if(it2 == it1)

                    {

                    continue;

                }

                //斜率不存在

                else if(it2->x == it1->x)

                    {

                    k1[position] += 1;

                }

                //斜率存在

                else

                    {

                    Point p2(it2->x,it2->y);

                    double k = (p2.y - p1.y)/(p2.x - p1.x);

                    map<double,int>::iterator it3 = (k0+position).find(k);

          if( it = map.end() )

                    {

                        (k0+position)[k] = 1;

                    )

                    else{

                        (k0+position)[k] = (k0+position)[k]+1;

                    }

                }   

            }

            position++;

        }

            

        for(int i1 = 0; i1<points.size();i1++)

            {

            for(map<double,int>::iterator it4 = k0[i1].begin(); it4 != k0[i1].end();it4++)

                {

                if(it4->second > k_maxnum)k_maxnum = it4->second;

            }

        }

            //int k1[points.size()] = {0};

        for(int i2 = 0; i2<points.size(); i2++)

            {

            if(k1[i2] > k_maxnum)k_maxnum = k1[i2];

        }

            

        return k_maxnum;

    }

        

        

        

        

        

};

 

2.

class Solution {

public:

    int maxPoints(vector<Point> &points) {

        int size = points.size();

        if(size == 0)

            return 0;

        else if(size == 1)

            return 1;

             

        int ret = 0;

        for(int i = 0;i<size;i++){

             

            int curmax = 1;

            map<double,int>mp;

            int vcnt = 0; //垂直点

            int dup = 0; //重复点

            for(int j = 0;j<size;j++){

                 

                if(j!=i){

                    double x1 = points[i].x - points[j].x;

                    double y1 = points[i].y - points[j].y;

                    if(x1 == 0 && y1 == 0){   //重复

                        dup++;

                    }else if(x1 == 0){      //垂直

                        if(vcnt == 0)

                            vcnt = 2;

                        else

                            vcnt++;

                        curmax = max(vcnt,curmax);

                    }else{

                        double k = y1/x1;          //斜率

                        if(mp[k] == 0)

                            mp[k] = 2;

                        else

                            mp[k]++;

                        curmax = max(mp[k],curmax);

                    }                   

                }

            }

            ret = max(ret,curmax+dup);           

        }

        return ret;

         

    }

};

0 0