-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidSquare.java
39 lines (29 loc) · 932 Bytes
/
ValidSquare.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
package math;
import utils.LeetCode;
import utils.Level;
import java.util.HashSet;
import java.util.Set;
@LeetCode(
no=593,
url = "https://leetcode.com/problems/valid-square/",
level = Level.MEDIUM,
title = "Valid Square"
)
public class ValidSquare {
public boolean validSquare(int[] a, int[] b, int[] c, int[] d) {
Set<Integer> points = new HashSet<>();
points.add(distanceOfTwoPoints(a,b));
points.add(distanceOfTwoPoints(a,c));
points.add(distanceOfTwoPoints(a,d));
points.add(distanceOfTwoPoints(b,c));
points.add(distanceOfTwoPoints(b,d));
points.add(distanceOfTwoPoints(c,d));
return !points.contains(0) && points.size() == 2;
}
int distanceOfTwoPoints(int []p1,int []p2) {
return (
(p1[0]-p2[0])*(p1[0]-p2[0])
+ (p1[1]-p2[1])*(p1[1]-p2[1])
);
}
}