当前位置:网站首页>【593. Valid Square】
【593. Valid Square】
2022-07-29 21:26:00 【[email protected]】
来源:力扣(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
方法:数学
思路与算法
The square determination theorem is a determination theorem used in geometry to determine whether a quadrilateral is a square.The general order for identifying a square is to first state that it is a parallelogram;Again, it's a rhombus(或矩形);The last statement is that it is a rectangle(或菱形).Then we can judge by enumerating the two hypotenuses of the quadrilateral:
- If the midpoints of the two hypotenuses are the same:Then it shows that the quadrilateral formed by the two hypotenuses is 「平行四边形」.
- 在满足「条件一」的基础上,If the two hypotenuses are the same length:Then it shows that the quadrilateral formed by the two hypotenuses is 「矩形」.
- 在满足「条件二」的基础上,If the two hypotenuses are perpendicular to each other:Then it shows that the quadrilateral formed by the two hypotenuses is 「正方形」.
代码:
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),仅使用常数变量.
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/210/202207292015287238.html
边栏推荐
- 940. 不同的子序列 II
- internship:利用easypoi将excel表数据导入导出
- Mass data query scheme mysql_Mysql massive data storage and solution 2 - Mysql sub-table query massive data... [easy to understand]
- 关于论青少年尽早学少儿编程之说
- MySQL - 设计游戏用户信息表
- :style中颜色使用函数动态获取赋值
- Safe Browser will have these hidden features that will let you play around with your browser
- JMeter使用教程(一)
- [GXYCTF2019]禁止套娃
- leetcode:952. 按公因数计算最大组件大小【并查集】
猜你喜欢

图床软件要收费,算了我自己写一个开源免费的。

嵌入式分享合集24

C# WPF给综合实战项目加个帮助文档

MySQL数据查询 - 简单查询

C#笔记 之 Oracle.ManagedDataAccess包的安装及配置

聚丙烯微孔膜的等离子体改性及DNA|有机自由基改性DNA-阳离子脂质复合体的应用

荧光量子点修饰siRNA-QDs|纳米金修饰siRNA-Au(RNA修饰方式方法)

分析少年派2中的Crypto

R language for airbnb data nlp text mining, geography, word cloud visualization, regression GAM model, cross-validation analysis

Single-core browser and what is the difference between dual-core browser, which to use?
随机推荐
一线技术人应该关注的四种思维能力
人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准
进程间六种通信方式
RNA的化学修饰原理|Gal-PEG-siRNA|siRNA-S-S-DSPE|siRNA-s-s-PEG|cholesterol-siRNA
LeetCode 0144. 二叉树的前序遍历:二叉树必会题
conda虚拟环境 | install 与 list 问题
指定宽度截取字符串
Sasser virus source code (ransomware source code)
ES6用法,面试大全
如何优雅的自定义 ThreadPoolExecutor 线程池
Where is Naive Bayes "naive"?
4. SAP ABAP OData 服务 Data Provider Class 的 GET_ENTITYSET 方法实现指南
Common power symbols meaning sharing
海量数据查询方案mysql_Mysql海量数据存储和解决方案之二—-Mysql分表查询海量数据…[通俗易懂]
剑指 Offer II 097. 子序列的数目
一道菜撑起百亿估值的太二酸菜鱼,能否迈过食品安全这道坎?
Samba服务器配置(什么情况下需要服务器)
怎么实现您的个人知识库?
Internship: use easypoi to import and export excel table data
图床软件要收费,算了我自己写一个开源免费的。