import java.util.Set;
import java.util.HashSet;
class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
Point()
{
}
}
// @Override
// public boolean equals(Object obj) {
// if (obj == null) {
// return false;
// }
// MyPoint myPoint = (MyPoint) obj;
// if (this.x == myPoint.x && this.y == myPoint.y) {
// return true;
// } else {
// return false;
// }
// }
// @Override
// public int hashCode() {
// return this.x+this.y;
// }
class Line {
public Set<Point> linePoints = new HashSet<Point>();
public Point A;
public Point B;
public Line(Point A, Point B) {
this.A = A;
this.B = B;
linePoints.add(A);
linePoints.add(B);
}
public boolean isOneLine(int x, int y) {
long V1x = A.x - B.x;
long V1y = A.y - B.y;
long V2x = A.x - x;
long V2y = A.y - y;
long V3x = B.x - x;
long V3y = B.y - y;
long Z12 = V1x * V2y - V1y * V2x;//---------------
long Z13 = V1x * V3y - V1y * V3x;
long Z23 = V2x * V3y - V2y * V3x;
if (Z12 == 0 && Z13 == 0 && Z23 == 0) {
return true;
} else {
return false;
}
}
}
public class Solution {
private Line[] lines = null;
public int maxPoints(Point[] points) {
if (points.length == 0) {
return 0;
}
if (points.length == 1) {
return 1;
}
lines = new Line[points.length * points.length];
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
if(points[i].x!=points[j].x||points[i].y!=points[j].y)
{
lines[i * points.length + j] = new Line(points[i],points[j]);
}
}
}
for (int i = 0; i < lines.length; i++) {
for (int j = 0; j < points.length; j++) {
if (lines[i] != null && lines[i].isOneLine(points[j].x, points[j].y)) {
lines[i].linePoints.add(points[j]);
}
}
}
int max = 0;
boolean flag=true;
for (int i = 0; i < lines.length; i++) {
if (lines[i] != null && max < lines[i].linePoints.size()) {
flag=false;
max = lines[i].linePoints.size();
}
}
if(flag)
{
return points.length;
}
return max;
}
public static void main(String[] args) {
// System.out.println("xxx");
Solution sol = new Solution();
Point[] points = new Point[9];
//[[84,250],[0,0],[1,0],[0,-70],[0,-70],[1,-1],[21,10],[42,90],[-42,-230]]
points[0] = new Point(84, 250);
points[1] = new Point(0,0);
points[2] = new Point(1, 0);
points[3] = new Point(0, -70);
points[4] = new Point(0, -70);
points[5] = new Point(1, -1);
points[6] = new Point(21, 10);
points[7] = new Point(42, 90);
points[8] = new Point(-42, -230);
int max = sol.maxPoints(points);
System.out.println(max);
for (int i = 0; i < sol.lines.length; i++) {
if (sol.lines[i] != null) {
for (Point point : sol.lines[i].linePoints) {
System.out.println("(" + point.x + "," + point.y + ")");
}
System.out.println();
}
}
// Set<MyPoint> myPoints=new HashSet<MyPoint>();
// myPoints.add(new MyPoint(new Point(0,0)));
// myPoints.add(new MyPoint(new Point(0,0)));
// myPoints.add(new MyPoint(new Point(0,0)));
// System.out.println(myPoints.size());
}
}
注意int的乘法 65536*65536
猜你喜欢
转载自blog.csdn.net/zhoumingsong123/article/details/80198990
今日推荐
周排行