---
# System prepended metadata

title: 0593. Valid Square
tags: [Leetcode, Medium]

---

# 0593. Valid Square
###### tags: `Leetcode` `Medium`
Link: https://leetcode.com/problems/valid-square/
## 思路
算每两个点的距离放在set里面
最后应该只剩下一个边长和一个对角线长度
## Code
```java=
class Solution {
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
        Set<Integer> set = new HashSet<>();
        set.add(d(p1, p2));
        set.add(d(p1, p3));
        set.add(d(p1, p4));
        set.add(d(p2, p3));
        set.add(d(p2, p4));
        set.add(d(p3, p4));
        return !set.contains(0) && set.size()==2;
    }
    private int d(int[] p1, int[] p2){
        return (p2[0]-p1[0])*(p2[0]-p1[0])+(p2[1]-p1[1])*(p2[1]-p1[1]);
    }
}
```