# 直线上最多的点数

给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。

 

示例 1:

输入:points = [[1,1],[2,2],[3,3]]
输出:3

示例 2:

输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
输出:4

 

提示:

## template ```cpp #include using namespace std; 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 &points) { int ans = 0; for (int i = 0; i < points.size(); ++i) { map, int> m; int p = 1; for (int j = i + 1; j < points.size(); ++j) { if (points[i].x == points[j].x && (points[i].y == points[j].y)) { ++p; continue; } int dx = points[j].x - points[i].x; int dy = points[j].y - points[i].y; int d = gcd(dx, dy); ++m[{dx / d, dy / d}]; } ans = max(ans, p); for (auto it = m.begin(); it != m.end(); ++it) { ans = max(ans, it->second + p); } } return ans; } int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } }; ``` ## 答案 ```cpp ``` ## 选项 ### A ```cpp ``` ### B ```cpp ``` ### C ```cpp ```