-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
593.java
31 lines (31 loc) · 1.52 KB
/
593.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
__________________________________________________________________________________________________
sample 0 ms submission
public class Solution {
public double dist(int[] p1, int[] p2) {
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
}
public boolean check(int[] p1, int[] p2, int[] p3, int[] p4) {
double n1 = dist(p1,p2);
if (n1 == 0) return false;
double n2 = dist(p2, p3);
double n3 = dist(p3, p4);
double n4 = dist(p4, p1);
return n1 == n2 && n2 == n3 && n3 == n4 && dist(p1,p3)==dist(p2,p4) ;
}
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
return check(p1, p2, p3, p4) || check(p1, p3, p2, p4) || check(p1, p3, p4, p2);
}
}
__________________________________________________________________________________________________
sample 34400 kb submission
public class Solution {
public double dist(int[] p1, int[] p2) {
return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
}
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
int[][] p={p1,p2,p3,p4};
Arrays.sort(p, (l1, l2) -> l2[0] == l1[0] ? l1[1] - l2[1] : l1[0] - l2[0]);
return dist(p[0], p[1]) != 0 && dist(p[0], p[1]) == dist(p[1], p[3]) && dist(p[1], p[3]) == dist(p[3], p[2]) && dist(p[3], p[2]) == dist(p[2], p[0]) && dist(p[0],p[3])==dist(p[1],p[2]);
}
}
__________________________________________________________________________________________________