当前位置:网站首页>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.
边栏推荐
猜你喜欢
力扣刷题之有效的正方形(每日一题7/29)
Kyushu cloud as cloud computing standardization excellent member unit
934. The Shortest Bridge
勾股数元组 od js
Teach you how to configure Jenkins automated email notifications
Interprocess communication study notes
Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
STP选举(步骤+案列)详解
mysql 视图
I have been working in software testing for 3 years, how did I go from just getting started to automated testing?
随机推荐
C language applet -- common classic practice questions
Likou Daily Question - Day 46 - 704. Binary Search
Drools基本介绍,入门案例,基本语法
221. 最大正方形
leetcode-1161:最大层内元素和
真正的CTO,是一个懂产品的技术人
ShardingJDBC usage summary
rpm安装postgresql12
想要写出好的测试用例,先要学会测试设计
最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?
Word 表格跨页,仍然显示标题
两个有序数组间相加和的Topk问题
[WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
Gateway路由的配置方式
Basic introduction to ShardingJDBC
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
有没有可以做副业可以日入300元方法?
《云原生的本手、妙手和俗手》——2022全国新高考I卷作文
勾股数元组 od js
leetcode-952:按公因数计算最大组件大小