当前位置:网站首页>The effective square of the test (one question of the day 7/29)
The effective square of the test (one question of the day 7/29)
2022-07-31 01:51:00 【Lanzhou Qianfan】
The square that is the most effective for pressing the button(每日一题7/29)
I am using complete computational brute force for this question.
The topic address is as follows
链接
题目要求如下
给定2D空间中四个点的坐标 p1, p2, p3 和 p4,如果这四个点构成一个正方形,则返回 true .
点的坐标 pi 表示为 [xi, yi] .输入 不是 按任何顺序给出的.
一个 有效的正方形 有四条等边和四个等角(90度角).
来源:力扣(LeetCode)


这是基本的要求.其实呢!Prompt I did not use,Because I'm doing purely mathematical calculations,The feature of coordinates is used.比较暴力,The code is fast,But the efficiency is high.
下面是我的代码
class Solution {
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
boolean result = judge_finally(p1,p2,p3,p4);
return result;
}
public static boolean judge_finally(int[]p1,int[]p2,int[]p3,int[]p4) {
double v_1_2 = judge_rhomb(p1, p2);//Some possible distances are calculated here
double v2_3 = judge_rhomb(p2, p3);
double v3_4 = judge_rhomb(p3, p4);
double v1_4 = judge_rhomb(p1, p4);
double v_2_4 = judge_rhomb(p2, p4);
double v_1_3 = judge_rhomb(p1, p3);
//Here is a list of possible diagonals,And make a diagonal judgment,Including the coincidence of the four-point diagonal midpoints,Confirm the quad
//Equal distance and diagonal vertical constraints.This progressively constrains it to a square
//Note the constraint zero coordinates,So a constraint must be made,The diagonal distance is constrained here0
if (v_1_3 == v_2_4 && v_1_3 != 0&&judge_vertical(p1, p3, p2, p4)&judge_if_line(p1,p3,p2,p4)) {
return true;
}
if (v1_4 == v2_3 && v1_4 != 0&&judge_vertical(p1, p4, p2, p3)&judge_if_line(p1,p4,p2,p3)) {
return true;
}
if (v_1_2 == v3_4 && v_1_2 != 0&&judge_vertical(p1, p2, p3, p4)&judge_if_line(p1,p2,p3,p4)) {
return true;
}
return false;
}
public static double judge_rhomb(int[]a,int[]b)//Calculate the length of the line formed by the two coordinates
{
double x1 = ( Math.pow(a[0]-b[0],2)+Math.pow(a[1]-b[1],2));
return x1;
}
public static boolean judge_vertical(int[]a,int[]b,int[]c,int[]d)//判断直角
{
int v[] = {
a[0]-b[0],a[1]-b[1]};
int v1[] = {
c[0]-d[0],c[1]-d[1]};
return v[0]*v1[0]+v[1]*v1[1]==0;
}
public static boolean judge_if_line(int[]a,int[]b,int[]c,int[]d)//Determine whether four points can form a parallelogram
//If four points can form a parallelogram,The diagonal midpoints coincide
{
boolean bool_line_ = a[0] + b[0] == c[0] + d[0];
boolean bool_line__ = a[1]+b[1]==c[1]+d[1];
if (bool_line_&&bool_line__)
{
return true;
}
return false;
}
}

Reduction method,很基础的方法
调用判断.The vertical here uses the characteristics of the vector.
x1x2+y1y2=0,In this way, you can determine whether it is vertical or not.
Judging whether the four coordinate points constitute a parallelogram,It is necessary to judge whether the midpoints of the diagonals of the possible cases coincide,Then it is only necessary to calculate whether the horizontal and vertical coordinates are equal to each other.
The diagonals of a rhombus bisect each other perpendicularly,We just have to make the diagonals equal,It can be judged as a square.But be careful to do these given coordinate constraints.So I gave the diagonal length not for0.Because if the horizontal and vertical coordinates of the four points are 0的话,The above requirements are actually met.
Complete diagonal constraints.
边栏推荐
- 太阳能板最大面积 od js
- Are you still working hard on the limit of MySQL paging?
- 充电效果模拟
- PDF 拆分/合并
- Software Testing Defect Reporting - Definition, Composition, Defect Lifecycle, Defect Tracking Post-Production Process, Defect Tracking Process, Purpose of Defect Tracking, Defect Management Tools
- 如何在 go 程序中暴露 Prometheus 指标
- 软件测试基础接口测试-入门Jmeter,你要注意这些事
- Nacos
- 221. 最大正方形
- 加密生活,Web3 项目合伙人的一天
猜你喜欢

MySQL的安装教程(嗷嗷详细,包教包会~)

内网渗透——提权

12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche

MySQL的存储过程

Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things

第一学年课程期末考试

pycharm cannot run after renaming (error: can't open file...No such file or directory)

tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现

【微信小程序】一文带你了解数据绑定、事件绑定以及事件传参、数据同步

Path and the largest
随机推荐
How to expose Prometheus metrics in go programs
keep-alive缓存组件
如何在 go 程序中暴露 Prometheus 指标
STP选举(步骤+案列)详解
打印任务排序 js od华为
"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition
Multiplication, DFS order
软件测试报告有哪些内容?
uniapp使用第三方字体
What have I experienced when I won the offer of BAT and TMD technical experts?
MySQL stored procedure
The Meta Metaverse Division lost 2.8 billion in the second quarter, still want to continue to bet?Metaverse development has yet to see a way out
Gateway routing configuration
pc端判断当前使用浏览器类型
Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
有没有可以做副业可以日入300元方法?
Path and the largest
真正的CTO,是一个懂产品的技术人
太阳能板最大面积 od js
成为比开发硬气的测试人,我都经历了什么?