您的位置:首页 > 其它

LeetCode 149 Max Points on a Line

2016-12-13 21:25 489 查看
Given n points
on a 2D plane, find the maximum number of points that lie on the same straight line.

在一个平面上有n个点,求一条直线最多能够经过多少个这些点。

Runtime: 10
ms  beats 99.93% of java submissions

public int maxPoints(Point[] points) {
int n = points.length;
if (n < 2) return n;
int currentL, maxL = 2, x, y, dx, dy, overlap = 0, upperB = n;
for (int i = 0; i < upperB; i++) {
for (int j = i + 1; j < n; j++) {
currentL = 1;
x = points[i].y - points[j].y;
y = points[j].x - points[i].x;
if (x == 0 && y == 0) overlap++;
else {
currentL++;
for (int k = j + 1; k < n && currentL + n - k > maxL; k++) {
dx = points[k].x - points[i].x;
dy = points[k].y - points[i].y;
if (x * dx + y * dy == 0)
currentL++;
}
}
maxL = Math.max(currentL + overlap, maxL);
}
upperB = n - maxL;
overlap = 0;
}
return maxL;
}
https://discuss.leetcode.com/topic/3991/my-java-accepted-solution-for-your-reference-only-using-array
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: