[LC593]有效的正方形-Valid Square
题目描述
给定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的更多相关文章
- [Swift]LeetCode593. 有效的正方形 | Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [LeetCode] Valid Square 验证正方形
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 593. Valid Square
Problem statement: Given the coordinates of four points in 2D space, return whether the four points ...
- LC 593. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [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 ...
- 定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。(体现多态)
实现多态的三个条件:1.要有继承2.要有抽象方法重写3.用父类指针(引用)指向子类对象 重载重写重定义的区别: 1.重载:在同一个类中进行; 编译时根据参数类型和个数决定方法调用; 子类无法重载父类; ...
- 最大正方形 · 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 中保留一 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- Java常见面试真题之中级进阶(HashMap篇)
前言 本来想着给自己放松一下,刷刷博客,突然被几道面试题难倒!说说Hashtable 与 HashMap 的区别?HashMap 中的 key 我们可以使用任何类作为 key 吗?HashMap 的长 ...
- jenkins集成cucumber-resport报告
需要安装的jenkins插件-Cucumber Reports jenkins版本:2.273 jenkins插件下载地址:点击下载 下载插件后通过jenkins插件管理上传已下载好的插件即可 等待j ...
- Windows高级调试
文档摘要: 本书<Windows高级调试>主要讲解Windows高级调试技术和工具,包括调试器简介.调试器揭密.符号文件与源文件的管理.栈内存破坏.堆内存破坏.安全.进程间通信.资源泄漏. ...
- C#中的9个“黑魔法”
C#中的9个"黑魔法"与"骚操作" 我们知道C#是非常先进的语言,因为是它很有远见的"语法糖".这些"语法糖"有时过于好 ...
- 如何制作一个HTML页面的锁屏功能
如果后台一些界面比较敏感,希望主动或者被动的在人员不想暴露信息的情况下加一把锁,就是说避免信息一直在页面上暴露,可以使用"阅后即焚"这种思路,这种思路比较简单,显示了就过几秒删除, ...
- pytorch的四个hook函数
训练神经网络模型有时需要观察模型内部模块的输入输出,或是期望在不修改原始模块结构的情况下调整中间模块的输出,pytorch可以用hook回调函数来实现这一功能.主要使用四个hook注册函数:regis ...
- Memcached笔记——(一)安装&常规错误&监控
08年的时候接触过Memcached,当时还对它的客户端产品嗤之以鼻,毕竟手工代码没有各种ORM原生XML配置方便.尽管如此,Memcached现在已经成了服务器架构里不可或缺的一部分! 相关链接: ...
- java公式解析器学习与开发(1)
public class Evaluate { public static void main(String[] args) { Stack<String> ops = new Stack ...
- 四、FreeRTOS学习笔记-任务创建和删除
FreeRTOS的任务创建和删除 1,任务创建和删除的API函数(熟悉) 任务的创建和删除本质就是调用FreeRTOS的API函数 一.任务创建 动态创建任务:任务的任务控制块以及任务的栈空间所需的内 ...
- MySQL数据库设计规范(新)
目录 规范背景与目的 设计规范2.1 数据库设计2.1.1 库名2.1.2 表结构2.1.3 列数据类型优化2.1.4 索引设计2.1.5 分库分表.分区表2.1.6 字符集2.1.7 程序DAO层设 ...