题目:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
//给定二维平面上的n个点,求同一直线上的点的最大个数。
代码:
public class Solution {
public int maxPoints(Point[] points) {
int abx;
int aby;
int bcx;
int bcy;
if(points.length<=2)
return points.length;
int max=2;
for(int i=0;i<points.length;i++){
int num=0;
int temp=1;
for(int j=i+1;j<points.length;j++){
abx=points[i].x-points[j].x;
aby=points[i].y-points[j].y;
if(abx==0&&aby==0){
num++;
}else{
temp++;
for(int k=j+1;k<points.length;k++){
bcx=points[j].x-points[k].x;
bcy=points[j].y-points[k].y;
if(bcx*aby==bcy*abx){
temp++;
}
}
}
if(max<(temp+num)){
max=temp+num;
}
temp=1;
}
}
return max;
}
}
继续阅读与本文标签相同的文章
MySQL
Excel–如何将数字转换为小写中文和大写中文?
-
草图大师怎么垂直旋转?su软件垂直旋转模型的方法
2026-05-18栏目: 教程
-
数字技术让“诗和远方”融为一体
2026-05-18栏目: 教程
-
“成人网站”免费让人上钩,其背后有什么不为人知的猫腻,网友:不敢惹
2026-05-18栏目: 教程
-
网络互连技术之路由协议
2026-05-18栏目: 教程
-
信息技术助力物流行业转型升级
2026-05-18栏目: 教程
