593. Valid Square
Given the coordinates of four points in 2D space p1, p2, p3 and p4, return true if the four points construct a square.
The coordinate of a point pi is represented as [xi, yi]. The input is not given in any order.
A valid square has four equal sides with positive length and four equal angles (90-degree angles).
Example 1:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: trueExample 2:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Output: falseExample 3:
Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1]
Output: trueConstraints:
p1.length == p2.length == p3.length == p4.length == 2-104 <= xi, yi <= 104
# @lc code=start
using LeetCode
function valid_square(ps::Vector{Vector{Int}})
sort!(ps)
ps[1] + ps[4] != ps[2] + ps[3] && return false
sum((ps[1] .- ps[2]) .^ 2) != sum((ps[1] .- ps[3]) .^ 2) && return false
return true
end
# @lc code=endvalid_square (generic function with 1 method)
This page was generated using DemoCards.jl and Literate.jl.