# [leetcode] 149. Max Points on a Line

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

1. 题目没有说明所有点不重复，可能有重复的点；

2. 平行y轴的直线没有斜率，不能存入HashTable，要特殊处理。

`/** * 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 maxNum = 0;                if(points.size() == 0) return 0;    if(points.size() == 1) maxNum = 1;        for(int i=0; i<points.size(); i++) {    unordered_map<double, int> hash;    int sameX = 0;    //e.g. line x=5    int samePoint = 1;            for(int j=i+1; j<points.size(); j++) {    if(points[i].x == points[j].x && points[i].y == points[j].y) {    samePoint++;    }                else if(points[i].x == points[j].x) {    sameX++;                }                else {                    double k = (double)(points[j].y - points[i].y) / (double)(points[j].x - points[i].x);    if(hash.find(k) == hash.end()) hash[k] = 1;    else hash[k]++;                }            }    for(auto it = hash.begin(); it != hash.end(); it++) {        maxNum = max(maxNum, it->second + samePoint);    }    maxNum = max(maxNum, sameX + samePoint);        }                    return maxNum;    }};`

0 0