当前位置:网站首页>leetcode-593:有效的正方形
leetcode-593:有效的正方形
2022-07-29 20:37:00 【菊头蝙蝠】
题目
给定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
解题
方法一:几何运用题
#define getL(a,b) (a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]) //计算边长的平方
class Solution {
public:
int len=INT_MAX;
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
return cal(p1,p2,p3)&&cal(p1,p2,p4)&&cal(p2,p3,p4);
}
bool cal(vector<int>& p1,vector<int>& p2,vector<int>& p3){
int l1=getL(p1,p2);
int l2=getL(p1,p3);
int l3=getL(p2,p3);
bool ok = (l1==l2&&l1+l2==l3)||(l1==l3&&l1+l3==l2)||(l2==l3&&l2+l3==l1);//判断等腰直角三角形
if(!ok) return false;
if(len==INT_MAX) len=min(l1,l2);
else if(len==0||len!=min(l1,l2)) return false;//每个等腰直角三角形边长要相等才行
return true;
}
};
边栏推荐
- Liu Genghong, boys and girls, come here!Sports data analysis and mining!(with a full set of code and data sets)
- Cobaltstrike和BurpSuite桌面快捷配置
- 如何让 x == 1 && x == 2 && x == 3 等式成立
- 探索创客教育在线管理实施体系
- Cobaltstrike and BurpSuite desktop shortcut configuration
- 7 行代码搞崩溃 B 站,原因令人唏嘘!
- offsetwidth111[easy to understand]
- 4. Implementation Guide for GET_ENTITYSET Method of SAP ABAP OData Service Data Provider Class
- SwiftUI * @State 相关问题
- ES6用法,面试大全
猜你喜欢
随机推荐
SwiftUI * @State 相关问题
Minimal jvm source code analysis
如何让 x == 1 && x == 2 && x == 3 等式成立
一线技术人应该关注的四种思维能力
JSP Servlet JDBC MySQL CRUD Sample Tutorial
分布式之基石: 可靠性——What a tangled web we weave
如何进入董事会:给CIO的十条建议
Huawei laptop keyboard locked (how does the laptop keyboard light up)
手写dialog弹框
Related phrases include usage and collocation (include)
磁性层状双金属氢氧化物和酶-DNA复合物|聚乙烯亚胺-DNA复合物(PEI/DNA)|作用机理
Cobaltstrike and BurpSuite desktop shortcut configuration
干货!联邦学习中的合作均衡
Use the PostgreSQL GRANT command to modify permissions on various database objects
MySQL数据查询 - 联合查询
回归——岭回归
JS实现百叶窗特效
About the choice of x86, x64, x86_64, ARM 64, ARM 32 when installing software
WPF 实现抽屉菜单
关于 golang 错误处理的一些优化想法