当前位置:网站首页>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;
}
};
边栏推荐
- 酷客导航助你商场轻松购物,业务办理不迷茫
- 【数据库】mysql日期格式转换
- 4. Implementation Guide for GET_ENTITYSET Method of SAP ABAP OData Service Data Provider Class
- UDP协议详解
- The difference between analog, digital and switching
- 【593. 有效的正方形】
- [Database] mysql date format conversion
- ES6用法,面试大全
- 用对象字面量或Map替代Switch/if语句
- The difference between uri and url is simple to understand (what is the difference between uri and url)
猜你喜欢

1. Promise usage in JS, 2. The concept and usage of closures, 3. The difference between the four methods and areas of object creation, 4. How to declare a class

刘畊宏男孩女孩看过来!运动数据分析挖掘!(附全套代码和数据集)

一 JS中Promise用法、二闭包的概念与用法、三对象创建的四种方式与区区别、四 如何声明一个类
基于PaddleSpeech搭建个人语音听写服务

人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准
![LeetCode 593 Valid Squares [Math] HERODING's Road to LeetCode](/img/c2/34624c9c7693ba40d0b3724c0db611.png)
LeetCode 593 Valid Squares [Math] HERODING's Road to LeetCode

SAG1-MIC8复合DNA基因疫苗|新型脂质-HAP-DNA复合体|实验要求

MySQL Data Query - Union Query

从专业角度分析国内创客教育发展

940. 不同的子序列 II
随机推荐
Use the PostgreSQL GRANT command to modify permissions on various database objects
一线技术人应该关注的四种思维能力
336. 回文对
:class数组写法
Related phrases include usage and collocation (include)
mos管闩锁效应理解学习
如何优雅的自定义 ThreadPoolExecutor 线程池
OneNote 教程,如何在 OneNote 中做笔记?
南信大提出TIPCB,一个简单但有效的用于基于文本的人员搜索的基于部分的卷积baseline
940. Different subsequences II
LeetCode 0144. 二叉树的前序遍历:二叉树必会题
关于 golang 错误处理的一些优化想法
分布式限流 redission RRateLimiter 的使用及原理
如何进入董事会:给CIO的十条建议
Kotlin - Coroutine Scope CoroutineScope, Coroutine Builder CoroutineBuilder, Coroutine Scope Function CoroutineScope Functiom
太卷了,企业级的智慧物业系统,也完全开源....
GET_ENTITYSET Method Implementation Guide for SAP ABAP OData Service Data Provider Class
Looking for a job - a chat with my cousin
回归——分层回归
240. Searching 2D Matrix II
