当前位置:网站首页>LeetCode 593 有效的正方形[数学] HERODING的LeetCode之路
LeetCode 593 有效的正方形[数学] HERODING的LeetCode之路
2022-07-29 20:19:00 【HERODING23】

解题思路:
一道并不是很困难的数学题,有好几种解决方法,最简单的就是直接判断是否正方形,即只有四边相等,并且任意两边平行或者垂直,更简单的判断方法就是任意三点都是直角三角形,代码如下:
class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
return isRTriangle(p1, p2, p3) && isRTriangle(p1, p2, p4) && isRTriangle(p1, p3, p4) && isRTriangle(p2, p3, p4);
}
bool isRTriangle(vector<int>& p1, vector<int>& p2, vector<int>& p3) {
int l1 = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
int l2 = (p1[0] - p3[0]) * (p1[0] - p3[0]) + (p1[1] - p3[1]) * (p1[1] - p3[1]);
int l3 = (p2[0] - p3[0]) * (p2[0] - p3[0]) + (p2[1] - p3[1]) * (p2[1] - p3[1]);
if(l1 > l2 && l2 == l3 && l1 == l2 + l3 ||
l2 > l1 && l1 == l3 && l2 == l1 + l3 ||
l3 > l1 && l1 == l2 && l3 == l1 + l2) {
return true;
}
return false;
}
};
边栏推荐
- SAP ABAP OData 服务 Data Provider Class 的 GET_ENTITYSET 方法实现指南试读版
- 优惠券系统设计思想
- conda virtual environment | install and list problems
- 模拟量、数字量与开关量的区别
- Thesis writing strategy | how to write an academic research paper
- JMeter使用教程(二)
- 磁性层状双金属氢氧化物和酶-DNA复合物|聚乙烯亚胺-DNA复合物(PEI/DNA)|作用机理
- 诺氟沙星-DNA复合物|半乳糖化脂质体-聚阳离子-DNA复合物|注意事项
- 怎么实现您的个人知识库?
- 找工作那些事-和表弟的一次聊天
猜你喜欢
随机推荐
赶紧进来!!!带你认识C语言基本数据类型
About the choice of x86, x64, x86_64, ARM 64, ARM 32 when installing software
诺氟沙星-DNA复合物|半乳糖化脂质体-聚阳离子-DNA复合物|注意事项
Huawei laptop keyboard locked (how does the laptop keyboard light up)
Omni-channel e-commerce | How can well-known domestic cosmeceuticals seize the opportunity to achieve rapid growth?
微博账号奇葩逻辑产品设计
剑指 Offer II 097. 子序列的数目
Durable rules (persistent rules engine) learning notes
七个易犯的 IT 管理错误—以及如何避免
单核浏览器和双核浏览器有什么区别,哪个好用?
378. 有序矩阵中第 K 小的元素
Summary of scratch learning related materials
华为笔记本键盘锁住了(笔记本电脑键盘怎么亮起来)
Verilog的时间格式系统任务----$printtimescale、$timeformat
一道菜撑起百亿估值的太二酸菜鱼,能否迈过食品安全这道坎?
Kotlin - Coroutine Scope CoroutineScope, Coroutine Builder CoroutineBuilder, Coroutine Scope Function CoroutineScope Functiom
博世集团启动量子数字孪生计划
The difference between uri and url is simple to understand (what is the difference between uri and url)
嵌入式分享合集24
带你刷(牛客网)C语言百题(第四天)









