经典指数          
原因
1094
浏览数
0
收藏数
 

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

     举报   纠错  
 
切换
1 个答案

//提示运行时间超过限制,估计是计算斜率和逐个判断导致超时,但是目前没有想到其他好的方法。 

//求大神解救~

class Solution {

public:

    int maxPoints(vector &points) {

        double k;

        double b;

        int max=0;

        for(int i=0; i

        {

            for(int j=i+1; j

            {

                k=(points.at(i).y-points.at(j).y)/(points.at(i).x-points.at(j).x);

                b=points.at(i).y-k*points.at(i).x;

                int count=0;

                for(int p=0; p

                {

                    if(points.at(p).y==k*points.at(p).x+b)

                        count++;

                }

                if(max

                    max=count;             

            }

        }

        return max;

    }

};

 
切换
撰写答案
扫描后移动端查看本题