当前位置:网站首页>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-08-03 19:05:00 【Lanzhou Qianfan】
Effective squares for brushing questions (Daily Question 7/29)
I am using complete computational brute force for this question.
The subject address is as follows link
The subject requirements are as follows
Given the coordinates p1, p2, p3 and p4 of four points in 2D space, returns true if the four points form a square.The coordinates pi of a point are represented as [xi, yi] .The inputs are not given in any order.A valid square has four equal sides and four equal angles (90-degree angles).Source: LeetCode
This is a basic requirement.Actually!I didn't use the prompt, because I used a completely pure mathematical calculation and used the characteristics of coordinates.More violent, the code is fast and large, but the efficiency is high.
Below is my code
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 heredouble 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 lists the possible diagonals, and makes a judgment of the diagonals, including the coincidence of the midpoints of the four-point diagonals, and confirms the quadrilateral//Equal distance and diagonal vertical constraint.This progressively constrains it to a square//Pay attention to the constraint of the zero point coordinate, so a constraint must be made, here the diagonal distance is constrained to not be 0if (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)//Judgment right angle{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)//Judge whether four points can form a parallelogram//If four points can form a parallelogram, the midpoints of the diagonals 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;}}It boils down to the method, a very basic method call judgment.The vertical here uses the characteristics of the vector.x1x2+y1y2=0, so you can judge whether it is vertical or not.To judge whether the four coordinate points constitute a parallelogram, it is necessary to judge whether the midpoints of the diagonals of the possible cases coincide, so here it is only necessary to calculate whether the abscissa and ordinate coordinates are equal respectively.The diagonals of a rhombus are perpendicular to each other, and we can judge it as a square as long as the diagonals are equal.But be careful to do these given coordinate constraints.So I gave the diagonal length not 0.Because if the horizontal and vertical coordinates of the four points are all 0, the above requirements can actually be satisfied.
Fully diagonal constraints.
边栏推荐
猜你喜欢
随机推荐
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
How does MySQL permanently support Chinese input once and for all?
讯方实训云平台——加速教育高质量发展的“数字底座”!
阿里二面:多线程间的通信方式有几种?举例说明
阿里资深专家打造从零开始学架构,含阿里内部技术栈PPT、PFD实战
阿里巴巴政委体系-第九章、阿里政委启示录
使用安全浏览器将网页保存为pdf的方法步骤
Big guy, who is free to help me to see what the problem is, I just read MySQL source print, and I just came into contact with flink.
POJ 1465 Multiple(用BFS求能组成的n的最小倍数)
一文搞懂│php 中的 DI 依赖注入
力扣刷题之爬楼梯(7/30)
字节跳动三面拿offer:网络+IO+redis+JVM+GC+红黑树+数据结构,助你快速进大厂!!
With the help of Kubernetes kubekey speed installation
MySQL——增删改查进阶
flex布局
金鱼哥RHCA回忆录:CL210管理计算资源--管理计算节点+章节实验
PreFixSum前缀和
【HCIP】MPLS实验
dd命令:用于读取、转换并输出数据
【ORACLE】什么时候ROWNUM等于0和ROWNUM小于0,两个条件不等价?


![[笔记]机器学习之前言介绍](/img/69/e2acd3efd5f513c9c32fca701b66c0.png)






