题目描述

给定2D空间中四个点的坐标 p1, p2, p3 和 p4,如果这四个点构成一个正方形,则返回 true 。

点的坐标 pi 表示为 [xi, yi] 。输入 不是 按任何顺序给出的。

一个 有效的正方形 有四条等边和四个等角(90度角)。

链接:https://leetcode.cn/problems/valid-square

基本思路

正方形的一个集合特点是,任意三个点都可以组成一个等腰直角三角形,等腰直角三角形是易于判断的,那么我们可以按顺序的选择四个点中的三个,依次去检测是否是等腰三角形,即可确认最后四个点是否可以组成正方形。

代码

Golang
func validSquare(p1 []int, p2 []int, p3 []int, p4 []int) bool {
ok, len := isTriangle(p1, p2, p3, -1)
if !ok {
return false
}
ok, len = isTriangle(p4, p2, p3, len)
if !ok {
return false
}
ok, len = isTriangle(p1, p4, p3, len)
if !ok {
return false
}
ok, _ = isTriangle(p1, p2, p4, len)
return ok
} func min(i1 int, i2 int) int {
if i1 > i2 {
return i2
}
return i1
} func getDist(p1 []int, p2 []int) int {
return (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1])
} func isTriangle(p1 []int, p2 []int, p3 []int, len int) (bool, int) {
l1 := getDist(p1, p2)
l2 := getDist(p2, p3)
l3 := getDist(p1, p3)
ok := l1+l2 == l3 || l1+l3 == l2 || l2+l3 == l1
if !ok {
return false, -1
}
if len == -1 {
len = min(l1, l2)
} else if len == 0 || len != min(l1, l2) {
return false, -1
}
return ok, len
}

[LC593]有效的正方形-Valid Square的更多相关文章

  1. [Swift]LeetCode593. 有效的正方形 | Valid Square

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  2. [LeetCode] Valid Square 验证正方形

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  3. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  4. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  5. 593. Valid Square

    Problem statement: Given the coordinates of four points in 2D space, return whether the four points ...

  6. LC 593. Valid Square

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  7. [Swift]LeetCode221. 最大正方形 | Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

  8. 定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。(体现多态)

    实现多态的三个条件:1.要有继承2.要有抽象方法重写3.用父类指针(引用)指向子类对象 重载重写重定义的区别: 1.重载:在同一个类中进行; 编译时根据参数类型和个数决定方法调用; 子类无法重载父类; ...

  9. 最大正方形 · Maximal Square

    [抄题]: 在一个二维01矩阵中找到全为1的最大正方形 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 返回 4 [暴力解法]: 时间分析: 空间分析:i j 中保留一 ...

  10. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. WPF学习-布局

    1.  Grid布局 ,(Table 布局) 两行两列布局, Border  0 行 0 列默认开始 <Window x:Class="WpfApp.MainWindow" ...

  2. 2-5 C++ 类型别名与自动类型

    目录 2.5.1 类型别名(Type Alias) typedef using 2.5.2 auto类型说明符 基本说明 注意点 2.5.3 decltype类型说明符 基本说明 注意点 2.5.1 ...

  3. games101_Homework0

    给定一个点 P=(2,1), 将该点绕原点先逆时针旋转 45 ◦,再平移 (1,2), 计算出 变换后点的坐标(要求用齐次坐标进行计算). 作业解答: #include<cmath> #i ...

  4. mysql热迁移

    0.背景 XtraBackup 优势 在线热备:支持在不停止数据库的情况下进行 InnoDB 和 XtraDB 的热备份,适合高可用环境. 增量备份:支持增量备份,能够显著减少备份时间和存储空间需求. ...

  5. Mac 中 NSTrackingArea 鼠标移动事件捕获

    在Mac系统中处理View的鼠标进入,退出以及移动事件时,需要把捕获的区域设置为view的bounds 不能设置为frame. 1 self.trackingArea = [[[NSTrackingA ...

  6. TypeScript名词解释系列:tsconfg中的target,module和moduleResolution

    tsconfg中的target,module和moduleResolution target 就是TypeScript文件编译后生成的javascript文件里的语法应该遵循哪个JavaScript的 ...

  7. Element UI 表格排序所有页

    Element UI 表格排序只排了当前页,解决方案如下: 定义方法: /** 比较 * @param {string} propertyName 属性值 * @param {string} sort ...

  8. 使用<a-select>时,placeholder不起作用

    当绑定v-model的值之后,placeholder设置的值不起作用,此时需要把v-model绑定的值设置为undefined就可以了

  9. Codeforces Round 895 (Div. 3)

    B. The Corridor or There and Back Again 题解 考虑二分答案 \(check\)时判断是否\(s_i \leq 2*(k - d_i),k\geq d_i\) c ...

  10. onlyoffice

    https://helpcenter.onlyoffice.com/installation/docs-enterprise-install-centos.aspx?_ga=2.51626159.76 ...