LeetCode: Max Points on a Line

来源:互联网 发布:java list排序算法 编辑:程序博客网 时间:2024/05/20 00:17

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 max = 0;        if(points.empty())            return 0;        for(int i = 0; i < points.size(); i++)        {            map<float, int> slopeMap;            int numOfEqual = 0, numOfSamePoint = 0;            float slope = 0;            int slopeArray[1000];            for(int j = 0; j < points.size(); j++)            {                if(i == j)                    continue;                if(points[i].x == points[j].x && points[i].y != points[j].y)                {                    numOfEqual++;                    if(numOfEqual > max)                        max = numOfEqual;                }                else if(points[i].x == points[j].x && points[i].y == points[j].y)                {                    numOfSamePoint++;                    if(numOfSamePoint > max)                        max = numOfSamePoint;                }                else                {                    slope = (float)(points[i].y - points[j].y)/(float)(points[i].x - points[j].x);                    if(++slopeMap[slope] + numOfSamePoint> max)                        max = slopeMap[slope] + numOfSamePoint;                }                            }        }        return ++max;            }};

Round 2:

/** * 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) {        if(points.empty())            return 0;        int max = 0;        for(int i = 0; i < points.size(); i++)        {            int numOfSamePoint = 0, numOfSameXPoint = 0;            unordered_map<float, int> m_map;            for(int j = 0; j < points.size(); j++)            {                if(i == j)                    continue;                if(points[i].x == points[j].x && points[i].y == points[j].y)                {                    numOfSamePoint++;                    if(numOfSamePoint > max)                        max = numOfSamePoint;                }                else if(points[i].x == points[j].x && points[i].y != points[j].y)                {                    numOfSameXPoint++;                    if(numOfSameXPoint > max)                        max = numOfSameXPoint;                }                else                {                    float slope = (float)(points[j].y - points[i].y)/(float)(points[j].x - points[i].x);                    m_map[slope]++;                    if(m_map[slope] + numOfSamePoint > max)                        max = m_map[slope] + numOfSamePoint;                }            }        }        return max+1;            }};


0 0
原创粉丝点击