//所用时间较短
import java.util.*;
/*
* public class Point {
* int x;
* int y;
* }
*/
public class Solution {
/**
*
* @param points Point类一维数组
* @return int整型
*/
public int maxPoints (Point[] points) {
// write code here
if(points == null || points.length == 0) //点的数组为空则返回0
return 0;
if(points.length <3) //若数组的长度小于3,则直接返回长度,只有两点必定在一条直线上
return points.length;
int samepoint ; //相同位置的点
int max = 2;
int sameSlopePoint; //相同斜率的点
for(int i=0;i<points.length;i++) {
samepoint = 0;
for(int j=i+1;j<points.length;j++) {
sameSlopePoint =1; //第一个点加上来
int Xdiatance = points[i].x - points[j].x; // X轴两点间的距离
int Ydiatance = points[i].y - points[j].y; // Y轴两点间的距离
if(Xdiatance == 0 && Ydiatance == 0) {
samepoint++;
}else {
sameSlopePoint = 2;
for(int k = j+1;k < points.length; k++) {
int Xdiatance2 = points[i].x - points[k].x;
int Ydiatance2 = points[i].y - points[k].y;
if(Xdiatance * Ydiatance2 == Ydiatance * Xdiatance2) {//乘法表示避免除法分母不为零的问题
sameSlopePoint++;
}
}
}
if(max < sameSlopePoint + samepoint) {
max = sameSlopePoint + samepoint;
}
}
}
return max;
}
}
import java.util.*;
/*
* public class Point {
* int x;
* int y;
* }
*/
public class Solution {
/**
*
* @param points Point类一维数组
* @return int整型
*/
public int maxPoints (Point[] points) {
// write code here
if(points == null || points.length == 0) //点的数组为空则返回0
return 0;
if(points.length <3) //若数组的长度小于3,则直接返回长度,只有两点必定在一条直线上
return points.length;
int samepoint ; //相同位置的点
int max = 2;
int sameSlopePoint; //相同斜率的点
for(int i=0;i<points.length;i++) {
samepoint = 0;
for(int j=i+1;j<points.length;j++) {
sameSlopePoint =1; //第一个点加上来
int Xdiatance = points[i].x - points[j].x; // X轴两点间的距离
int Ydiatance = points[i].y - points[j].y; // Y轴两点间的距离
if(Xdiatance == 0 && Ydiatance == 0) {
samepoint++;
}else {
sameSlopePoint = 2;
for(int k = j+1;k < points.length; k++) {
int Xdiatance2 = points[i].x - points[k].x;
int Ydiatance2 = points[i].y - points[k].y;
if(Xdiatance * Ydiatance2 == Ydiatance * Xdiatance2) {//乘法表示避免除法分母不为零的问题
sameSlopePoint++;
}
}
}
if(max < sameSlopePoint + samepoint) {
max = sameSlopePoint + samepoint;
}
}
}
return max;
}
}