当前位置:网站首页>【593. 有效的正方形】
【593. 有效的正方形】
2022-07-29 20:17:00 【千北@】
来源:力扣(LeetCode)
描述:
给定2D空间中四个点的坐标 p1
, p2
, p3
和 p4
,如果这四个点构成一个正方形,则返回 true
。
点的坐标 pi
表示为 [xi, yi]
。输入 不是 按任何顺序给出的。
一个 有效的正方形 有四条等边和四个等角(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
提示:
- p1.length == p2.length == p3.length == p4.length == 2
- -104 <= xi, yi <= 104
方法:数学
思路与算法
正方形判定定理是几何学里用于判定一个四边形是否为正方形的判定定理。判别正方形的一般顺序为先说明它是平行四边形;再说明它是菱形(或矩形);最后说明它是矩形(或菱形)。那么我们可以从枚举四边形的两条斜边入手来进行判断:
- 如果两条斜边的中点相同:则说明以该两条斜边组成的四边形为「平行四边形」。
- 在满足「条件一」的基础上,如果两条斜边的长度相同:则说明以该两条斜边组成的四边形为「矩形」。
- 在满足「条件二」的基础上,如果两条斜边的相互垂直:则说明以该两条斜边组成的四边形为「正方形」。
代码:
class Solution {
public:
bool checkLength(vector<int>& v1, vector<int>& v2) {
return (v1[0] * v1[0] + v1[1] * v1[1]) == (v2[0] * v2[0] + v2[1] * v2[1]);
}
bool checkMidPoint(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
return (p1[0] + p2[0]) == (p3[0] + p4[0]) && (p1[1] + p2[1]) == (p3[1] + p4[1]);
}
int calCos(vector<int>& v1, vector<int>& v2) {
return (v1[0] * v2[0] + v1[1] * v2[1]) == 0;
}
bool help(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
vector<int> v1 = {
p1[0] - p2[0], p1[1] - p2[1]};
vector<int> v2 = {
p3[0] - p4[0], p3[1] - p4[1]};
if (checkMidPoint(p1, p2, p3, p4) && checkLength(v1, v2) && calCos(v1, v2)) {
return true;
}
return false;
}
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
if (p1 == p2) {
return false;
}
if (help(p1, p2, p3, p4)) {
return true;
}
if (p1 == p3) {
return false;
}
if (help(p1, p3, p2, p4)) {
return true;
}
if (p1 == p4) {
return false;
}
if (help(p1, p4, p2, p3)) {
return true;
}
return false;
}
};
执行用时:4 ms, 在所有 C++ 提交中击败了75.76%的用户
内存消耗:25.8 MB, 在所有 C++ 提交中击败了69.97%的用户
复杂度分析
时间复杂度:O(1)。
空间复杂度:O(1),仅使用常数变量。
边栏推荐
- RNA修饰质谱检测|dextran-siRNA 葡聚糖化学偶联DNA/RNA|siRNA-PLGA聚乳酸-羟基乙酸共聚物修饰核糖核酸
- json-c实现json和结构体之间的相互转换
- JMeter usage tutorial (2)
- [mathematical foundation] probability and mathematical statistics related concept learning
- mysql get field comments and get table fields
- 七个易犯的 IT 管理错误—以及如何避免
- Expert advice | How to formulate a growth strategy for survival in an economic downturn
- 指定宽度截取字符串
- OAuth,JWT ,OIDC你们搞得我好乱啊
- SwiftUI * @State 相关问题
猜你喜欢
剑指 Offer II 097. 子序列的数目
赶紧进来!!!带你认识C语言基本数据类型
使用MD5加密后的字符串存密码安全吗?你不得不了解的Hash算法
探索创客教育在线管理实施体系
mos管闩锁效应理解学习
4D Summary: 38 Knowledge Points of Distributed Systems
JSP Servlet JDBC MySQL CRUD Sample Tutorial
PEG-PEI共聚物/DNA复合物|甘草次酸修饰的长循环阳离子脂质体DNA复合物|解析说明
尿素偶联Urea-siRNA Conjugates|Cyclodextrin-siRNA-β-CD环糊精修饰RNA核酸(解析说明)
In the past six months, I have done those things about the automatic return of the transaction link...
随机推荐
scratch programming + elementary math
C#笔记 之 Oracle.ManagedDataAccess包的安装及配置
About the choice of x86, x64, x86_64, ARM 64, ARM 32 when installing software
学校安全管理专题培训实施方案
Briefly talk about K-means clustering
Single-core browser and what is the difference between dual-core browser, which to use?
JMeter使用教程(一)
SAP ABAP OData 服务 Data Provider Class 的 GET_ENTITYSET 方法实现指南试读版
:class数组写法
JMeter usage tutorial (2)
Omni-channel e-commerce | How can well-known domestic cosmeceuticals seize the opportunity to achieve rapid growth?
尿素偶联Urea-siRNA Conjugates|Cyclodextrin-siRNA-β-CD环糊精修饰RNA核酸(解析说明)
分布式限流 redission RRateLimiter 的使用及原理
错误解决:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255]
进程间六种通信方式
Unity determines whether a string can be converted to float type
json-c实现json和结构体之间的相互转换
[mathematical foundation] probability and mathematical statistics related concept learning
Where is Naive Bayes "naive"?
如何优雅的自定义 ThreadPoolExecutor 线程池