-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
593 Valid Square.swift
49 lines (42 loc) 路 1.47 KB
/
593 Valid Square.swift
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
47
48
49
//
// 593 Valid Square.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 11/11/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/valid-square/
class Solution {
private typealias Point = (x: Int, y: Int)
/// Finds if the four points could construct a square.
///
/// - Parameters:
/// - p1: First point.
/// - p2: Second point.
/// - p3: Third point.
/// - p4: Fourth point.
/// - Returns: True if the four points could construct a square, otherwise
/// returns false.
///
/// - Complexity:
/// - time: O(1), only constant time is used.
/// - space: O(1), only constant space is used.
func validSquare(_ p1: [Int], _ p2: [Int], _ p3: [Int], _ p4: [Int]) -> Bool {
let p1 = Point(p1[0], p1[1])
let p2 = Point(p2[0], p2[1])
let p3 = Point(p3[0], p3[1])
let p4 = Point(p4[0], p4[1])
return check(p1, p2, p3, p4) || check(p1, p3, p2, p4) || check(p1, p2, p4, p3)
}
private func check(_ p1: Point, _ p2: Point, _ p3: Point, _ p4: Point) -> Bool {
dist(p1,p2) > 0 &&
dist(p1, p2) == dist(p2, p3) &&
dist(p2, p3) == dist(p3, p4) &&
dist(p3, p4) == dist(p4, p1) &&
dist(p1, p3) == dist(p2, p4)
}
private func dist(_ p1: Point, _ p2: Point) -> Int {
(p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)
}
}