当前位置:网站首页>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;
}
};
边栏推荐
- 回归——分层回归
- Second Best PyTorch Beginner Course; Thesis Writing Guide; Using µGo to Develop a Mini Compiler; Super Efficient Use of Transformer's Extension Library; Frontier Papers | ShowMeAI News Daily
- 最小jvm源码分析
- The difference between uri and url is simple to understand (what is the difference between uri and url)
- offsetwidth111[easy to understand]
- 刘畊宏男孩女孩看过来!运动数据分析挖掘!(附全套代码和数据集)
- 太卷了,企业级的智慧物业系统,也完全开源....
- MySQL数据查询 - 联合查询
- 赶紧进来!!!带你认识C语言基本数据类型
- 从实例学Kettle(一):获取股票行情数据
猜你喜欢

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

Writing Elegant Kotlin Code: Talk About What I Think "Kotlinic"

分布式限流 redission RRateLimiter 的使用及原理

336. 回文对

OAuth,JWT ,OIDC你们搞得我好乱啊

MySQL Data Query - Simple Query

【Nacos】nacos1.x 单机、内置数据库模式修改密码

Setinel 原理简介

240. Searching 2D Matrix II

WeChat Mini Program 31 Subcontracting Mechanism
随机推荐
378. The Kth Smallest Element in an Ordered Matrix
The demand for VR live broadcast marketing is increasing, and the data module is paving the way for us
Sasser virus source code (ransomware source code)
The difference between analog, digital and switching
根据昵称首字母生成头像
LeetCode 0144. 二叉树的前序遍历:二叉树必会题
怎么实现您的个人知识库?
无文件落地免杀的初尝试思考(上)
JS实现百叶窗特效
LeetCode--single linked list--146.LRU cache
7 行代码搞崩溃 B 站,原因令人唏嘘!
Come in now!!!Take you to know the basic data types of C language
高通WLAN框架学习(31)-- Power save
手写dialog弹框
南信大提出TIPCB,一个简单但有效的用于基于文本的人员搜索的基于部分的卷积baseline
LeetCode--单链表--146.LRU缓存
JUC Concurrent Programming Basics AQS
mdnice-test
Permutations of a small feat: cantor
Oracle问题: ORA-01882: 未找到时区
