当前位置:网站首页>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.
边栏推荐
- C#将位图旋转90度
- MySQL如何 drop 大表
- LeetCode 952. 按公因数计算最大组件大小
- 梅科尔工作室-14天华为培训七
- Postgresql source code (65) analysis of the working principle of the new snapshot system Globalvis
- 不要小看 WebSocket!长连接、有状态、双向、全双工都是王炸技能
- 开发即时通讯到底需要什么样的技术,需要多久的时间
- SQL server 实现触发器备份表数据
- Postgresql snapshot optimization Globalvis new system analysis (performance greatly enhanced)
- 货比四家 version tb1.63
猜你喜欢
Radondb mysql installation problems
LeetCode 622. 设计循环队列
红日安全内网渗透靶场-VulnStack-1
首届MogDB征文活动开启啦!
Bytes to beat three sides take offer: network + GC + + IO + redis + JVM red-black tree + data structure, to help you quickly into the giant!!!!!
盲僧发现了华点——教你如何使用API接口获取数据
阿里巴巴政委体系-第六章、阿里政委体系运作
Difference差分数组
梅科尔工作室-14天华为培训六
Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义
随机推荐
【C语言学习笔记(五)】while循环与for循环
剑指Offer 56.数组中数字出现的次数
读取 resources 目录下的文件路径的九种方式,你知道多少?
Confused!Ali was abused on the one hand, but was fortunate to be promoted to Huawei's technology, and successfully got the offer, with an annual salary of 40w
C#爬虫之通过Selenium获取浏览器请求响应结果
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃
Radondb mysql installation problems
【WPS-OFFICE-Word】 WPS中样式的运作原理?样式自动更新、自动改变如何处理?样式的管理方法?
阿里巴巴政委体系-第七章、阿里政委培育
WEB 渗透之SSRF
pytest接口自动化测试框架 | Jenkins集成初探
With the help of Kubernetes kubekey speed installation
首届MogDB征文活动开启啦!
WEB 渗透之RCE
【计网】二、物理层
LeetCode 622. 设计循环队列
tree命令:以树的形式列出目录中的文件
讯方实训云平台——加速教育高质量发展的“数字底座”!
Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义
Climbing Stairs (7/30)