[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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- rqt的安装及详细介绍
1. 安装 安装极其简单,不多介绍,直接上命令:Melodic: sudo apt-get install ros-melodic-rqt sudo apt-get install ros-melod ...
- 让性能提升56%的Vue3.5响应式重构之“版本计数”
前言 Vue3.5响应式重构主要分为两部分:双向链表和版本计数.在上一篇文章中我们讲了 双向链表 ,这篇文章我们接着来讲版本计数. 欧阳年底也要毕业了,加入欧阳的面试交流群(分享内推信息).高质量vu ...
- springboot的基本使用
SpringBoot简介 SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程. 使用了Spring框架后已经简化了我们的开发,而Spr ...
- 测试App出现闪退应对方法
1.使用adb logcat 查看日志,使用adb logcat -f sdcard/log.txt(输出到手机上) ,先查看报错信息 2.保证主线程能够继续运行,避免在子线程中与UI交互 3.内存溢 ...
- 狗的名字 ATCOER-ABC-171-C One Quadrillion and One Dalmatians
狗的名字 ATCOER-ABC-171-C One Quadrillion and One Dalmatians 题目链接 我们可以将名字看成26进制的数,就可以转化为将一个10进制转26进制的数的问 ...
- Navicat Premium 16下载与安装
1.可以通过以下两种方式下载 a.官网下载地址 https://www.navicat.com.cn/download/navicat-premium b.百度网盘下载地址 链接:https://pa ...
- 话说ReferenceQueue
也是几年前写的,在内部邮件列表里发过,在这里保存一下. 看到了这篇帖子: <WeakHashMap的神话>http://www.javaeye.com/topic/587995 因为Jav ...
- re模块 函数模式详解
re模块 python爬虫过程中,实现页面元素解析的方法很多,正则解析只是其中之一,常见的还有BeautifulSoup和lxml,它们都支持网页HTML元素解析,re模块提供了强大的正则表达式功能 ...
- 销讯通-CRM系统的功能远远不止于用来打卡
在信息化的过程中,CRM系统其实很多企业都在用,最开始的设想是很好的,大家用着之后发现它可能最终只会沦为一个上班打卡考核或者是最基础的一个签到工具了,没有发挥它应有的一个功能. 最基础的一个诉求 我们 ...
- 2024 盘古石数据取证 服务器部分wp
1. 分析内部IM服务器检材,在搭建的内部即时通讯平台中,客户端与服务器的通讯端口是:[答案格式:8888][★☆☆☆☆] 8065 2. 分析内部IM服务器检材,该内部IM平台使用的数据库版本是: ...