题目描述

给定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. hadoop运行原理

    包括HDFS和Mapreduce两部分. 1)HDFS自动保存多个副本,移动计算.缺点是小文件存取占用namenode内存,写入只支持追加,不能随机修改. 它存储的逻辑空间称为block,文件的权限类 ...

  2. 题解:「NOIP2022 提高组」种花

    题解:「NOIP2022 提高组」种花 题目大意:给定一个 \(n \times m\) 的01矩阵,0表示可以种花,1表示土坑(无法种花),现在要在图上种出一个C型或F型(C,F横着的两条线的长度都 ...

  3. 重温c语言之,7天开整,就是随便的写写,第三天+第四天版

    一:指针 1.关于指针的含义---粗略 例如:int a=10; int* p=&a; 这里的*,是说明p是指针变量,int 说明p是指向的对象是int类型的 *p=20, 这里的*是解引用符 ...

  4. 教程:搭建一个我的世界模组服务器(Linux)

    首先给自己的服务器打个广告 服务器版本1.12.2 地址:www.verysucksminecraftserver.top(好像只有一个月) 所需Mod网盘:https://pan.quark.cn/ ...

  5. HttpWebRequest调用API

    public void HttpWebRequestPost(){ string responseContent = string.Empty; var userNameAndPwd = new { ...

  6. FFmpeg转码音视频时间戳设置分析

    音频时间戳设置 以下代码基于FFmpeg n5.1.2进行分析 以下文档中有关音频的具体时间戳数据来自以下转码命令: ./ffmpeg_g -rw_timeout 5000000 -i 'rtmp:/ ...

  7. 低功耗4G模组HTTP网络协议应用

    ​ 大家好,今天我们来学习合宙Air780E模组LuatOS开发4G通信中HTTP网络协议的应用,实现模组和服务器之间数据的传输. 一.HTTP概述 1.1 简介 HTTP是HyperTextTran ...

  8. html代码新手教学

    HTML 是超文本标记语言(HyperText Markup Language)的缩写,是用来描述网页结构的标记语言.在这篇教学中,我们将介绍一些 HTML 基础知识,帮助新手快速学习并掌握如何编写简 ...

  9. commons.dbutils1.2介绍及使用

    一.结构介绍 高层结构图: wrappers包: handlers包(部分): 二.功能介绍 commons.dbutils是一个对JDBC操作进行封装的类集,其有如下几个优点: (1)没有可能的资源 ...

  10. AWMS(ApeosWare Management Suite)的一些配置信息

    富士胶片商业创新的富士施乐打印复印一体机,一般可用被称为亚特兰大(ApeosWare Management Suite)的管理软件. Windows Server 2012 上安装的SQLServer ...