-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
46 lines (46 loc) · 1.4 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
if (points.length<3) {
return points.length;
}
int answer=2;
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)){
int sum=0;
for (int k = 0; k < points.length; k++) {
if (isStraight(points[i],points[j],points[k])) {
sum++;
}
}
if (sum>answer) {
answer=sum;
}
}else {
int sum=0;
for (int k = 0; k < points.length; k++) {
if ((points[k].x==points[j].x)&&(points[k].y==points[j].y)) {
sum++;
}
}
if (sum>answer) {
answer=sum;
}
}
}
}
return answer;
}
public boolean isStraight (Point a,Point b,Point c){
return ((b.y-c.y)*(a.x-c.x))==((a.y-c.y)*(b.x-c.x));
}
}