题解 | #直线上的牛#
直线上的牛
https://www.nowcoder.com/practice/58ff4047d6194d2ca4d80869f53fa6ec
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param points int整型二维数组
* @return int整型
*/
public int maxPoints (int[][] points) {
// write code here
if (points.length <= 2) {
return points.length;
}
int result = 2;
for (int i = 0; i < points.length-2; i++) {
for (int j = i + 1; j < points.length-1; j++) {
int count = 2;
for (int k = j + 1; k < points.length; k++) {
if (points[i][0] == points[j][0]) {
if (points[k][0] == points[j][0]) {
count++;
}
} else if (points[i][1] == points[j][1]) {
if (points[k][1] == points[j][1]) {
count++;
}
} else {
if ((((points[k][1] - points[j][1]) / (points[k][0] - points[j][0])) == ((
points[i][1] - points[j][1]) / (points[i][0] - points[j][0])))) {
count++;
}
}
}
result = Math.max(result, count);
}
}
return result;
}
}
本题我采用的是暴力匹配,所用编程语言是java。
先随便找两个点构成一条直线,再看剩下的点有多少个可以与之前的两个点构成同一条直线

查看4道真题和解析