当前位置:网站首页>593. 有效的正方形 : 简单几何运用题
593. 有效的正方形 : 简单几何运用题
2022-07-29 11:31:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 593. 有效的正方形 ,难度为 中等。
Tag : 「模拟」、「数学」、「计算几何」
给定 2D
空间中四个点的坐标 p1
, p2
, p3
和 p4
,如果这四个点构成一个正方形,则返回 true
。
点的坐标 pi
表示为 。输入 不是 按任何顺序给出的。
一个 有效的正方形 有四条等边和四个等角(90
度角)。
示例 1:
输入: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
输出: True
示例 2:
输入:p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
输出:false
示例 3:
输入:p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1]
输出:true
提示:
计算几何
根据题意进行模拟即可。
从给定的 个顶点中选 个顶点,检查其能否形成「直角三角形」,同时保存下来首个直角三角形的直角边边长,供后续其余直角三角形进行对比(注意不能共点,即直角边长不能为 )。
Java 代码:
class Solution {
long len = -1;
public boolean validSquare(int[] a, int[] b, int[] c, int[] d) {
return calc(a, b, c) && calc(a, b, d) && calc(a, c, d) && calc(b, c, d);
}
boolean calc(int[] a, int[] b, int[] c) {
long l1 = (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
long l2 = (a[0] - c[0]) * (a[0] - c[0]) + (a[1] - c[1]) * (a[1] - c[1]);
long l3 = (b[0] - c[0]) * (b[0] - c[0]) + (b[1] - c[1]) * (b[1] - c[1]);
boolean ok = (l1 == l2 && l1 + l2 == l3) || (l1 == l3 && l1 + l3 == l2) || (l2 == l3 && l2 + l3 == l1);
if (!ok) return false;
if (len == -1) len = Math.min(l1, l2);
else if (len == 0 || len != Math.min(l1, l2)) return false;
return true;
}
}
TypeScript 代码:
let len = -1
function validSquare(a: number[], b: number[], c: number[], d: number[]): boolean {
len = -1
return calc(a, b, c) && calc(a, b, d) && calc(a, c, d) && calc(b, c, d)
};
function calc(a: number[], b: number[], c: number[]): boolean {
const l1 = (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1])
const l2 = (a[0] - c[0]) * (a[0] - c[0]) + (a[1] - c[1]) * (a[1] - c[1])
const l3 = (b[0] - c[0]) * (b[0] - c[0]) + (b[1] - c[1]) * (b[1] - c[1])
const ok = (l1 == l2 && l1 + l2 == l3) || (l1 == l3 && l1 + l3 == l2) || (l2 == l3 && l2 + l3 == l1)
if (!ok) return false
if (len == -1) len = Math.min(l1, l2)
else if (len == 0 || len != Math.min(l1, l2)) return false
return true
}
时间复杂度: 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.593
篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
- Qt 之自定义界面(实现无边框、可移动)
- DoD 和 DoR,消减「认知偏差」的两大神器
- Zhou Hongyi: 360 is the largest secure big data company in the world
- Is this it?TypeScript actually not difficult!(recommended collection)
- 使用anyio替代asyncio
- From scratch Blazor Server (3) - add cookie authorization
- 如何对SQuAD1.1数据集进行预处理「详解版」
- Deep understanding of c # nullable types
- Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life
- 一键搭建博客:如何使用WordPress插件搭建专属博客
猜你喜欢
如何使用 grep 跨多行查找模式匹配
游戏合作伙伴专题:BreederDAO 与《王国联盟》结成联盟
PyQt5快速开发与实战 6.6 QFormLayout(表单布局) && 6.7 嵌套布局 && 6.8 QSplitter
ES6-箭头函数this指向
Similarities and differences of QWidget, qdialog and qmainwindow
Proficient in audio and video development can really do whatever you want
AI全流程开发难题破解之钥
Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
自采集在线电脑壁纸php源码v2.0自适应端
2022年企业直播行业发展洞察
随机推荐
大伟 GBase8s游标稳定性读ESQL测试用例
Std:: vector copy, append, nested access
Sunwenlong, Secretary General of the open atom open source foundation, worked together to expand open source
DNS protocol, ICMP protocol, NAT technology
牛客网刷题
mysql单行,多行子查询
After connect and SQL join in on conditions and where
WeChat red envelope test case
基本.分块
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
大伟 Golang之路
QML(二):设置自定义窗体
[SwiftUI 开发] @State @Binding @ObservedObject @EnvironmentObject
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
浅谈string中的compareTo方法
️ 炒 股 实 战丨原 地 起 飞 ️
PaddleLite 编译以及代码跑通复盘
Deep understanding of c # nullable types
Deep understanding of c # delegate into the fast lanes
Design and implementation of gbase8s Informix dodker high availability cluster self recovery cluster startup command oninitdb