当前位置:网站首页>【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),仅使用常数变量。
边栏推荐
- Data visualization ---- web page displays temperature and humidity
- 用 Array.every & Array.some 匹配全部/部分内容 es6
- QT安装、创建项目与调试,在VS中的使用:手把手教程
- R语言对airbnb数据nlp文本挖掘、地理、词云可视化、回归GAM模型、交叉验证分析
- LeetCode_474_一和零
- 万字总结:分布式系统的38个知识点
- [mathematical foundation] probability and mathematical statistics related concept learning
- Thesis writing strategy | how to write an academic research paper
- 七个易犯的 IT 管理错误—以及如何避免
- Mass data query scheme mysql_Mysql massive data storage and solution 2 - Mysql sub-table query massive data... [easy to understand]
猜你喜欢

Baidu internship students late night fun: originally giant is this kind of life

Private domain growth | Private domain members: 15 case collections from 9 major chain industries

378. 有序矩阵中第 K 小的元素

百度实习学弟深夜吐槽:原来大厂是这种生活啊

叶酸&适配体修饰DNA纳米载体|CdS纳米颗粒修饰DNA|科研试剂

940. 不同的子序列 II

The second growth curve | The driving guide for corporate innovation to break through the stagnation dilemma

如何进入董事会:给CIO的十条建议

Single-core browser and what is the difference between dual-core browser, which to use?

mos管闩锁效应理解学习
随机推荐
找工作那些事-和表弟的一次聊天
这半年我做交易链路自动化回归的那些事儿...
藻酸盐/PEI/DNA复合载体|脂质-鱼精蛋白-DNA复合物|合成方法
五个供应商销售谈判策略的识别以及应对它们的方法
ESP8266-Arduino programming example-EEPROM read and write
R language for airbnb data nlp text mining, geography, word cloud visualization, regression GAM model, cross-validation analysis
怎么实现您的个人知识库?
RNA的化学修饰原理|Gal-PEG-siRNA|siRNA-S-S-DSPE|siRNA-s-s-PEG|cholesterol-siRNA
7 行代码搞崩溃 B 站,原因令人唏嘘!
Unity判断字符串是否可以转为float类型
offsetwidth111[easy to understand]
Verilog的时间格式系统任务----$printtimescale、$timeformat
用对象字面量或Map替代Switch/if语句
朴素贝叶斯“朴素”在哪里?
QT安装、创建项目与调试,在VS中的使用:手把手教程
回归——分层回归
安全浏览器将拥有这些隐藏功能,让你玩转浏览器
Safe Browser will have these hidden features that will let you play around with your browser
siRNA-S-S-PEG-LMWP|M-MSN-siRNA介孔二氧化硅修饰RNA(齐岳RNA功能化修饰)
解析掌握现代化少儿编程实操能力