题目描述

给定2D空间中四个点的坐标 p1, p2, p3 和 p4,如果这四个点构成一个正方形,则返回 true 。

点的坐标 pi 表示为 [xi, yi] 。输入 不是 按任何顺序给出的。

一个 有效的正方形 有四条等边和四个等角(90度角)。

链接:https://leetcode.cn/problems/valid-square

基本思路

正方形的一个集合特点是,任意三个点都可以组成一个等腰直角三角形,等腰直角三角形是易于判断的,那么我们可以按顺序的选择四个点中的三个,依次去检测是否是等腰三角形,即可确认最后四个点是否可以组成正方形。

代码

Golang
func validSquare(p1 []int, p2 []int, p3 []int, p4 []int) bool {
ok, len := isTriangle(p1, p2, p3, -1)
if !ok {
return false
}
ok, len = isTriangle(p4, p2, p3, len)
if !ok {
return false
}
ok, len = isTriangle(p1, p4, p3, len)
if !ok {
return false
}
ok, _ = isTriangle(p1, p2, p4, len)
return ok
} func min(i1 int, i2 int) int {
if i1 > i2 {
return i2
}
return i1
} func getDist(p1 []int, p2 []int) int {
return (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1])
} func isTriangle(p1 []int, p2 []int, p3 []int, len int) (bool, int) {
l1 := getDist(p1, p2)
l2 := getDist(p2, p3)
l3 := getDist(p1, p3)
ok := l1+l2 == l3 || l1+l3 == l2 || l2+l3 == l1
if !ok {
return false, -1
}
if len == -1 {
len = min(l1, l2)
} else if len == 0 || len != min(l1, l2) {
return false, -1
}
return ok, len
}

[LC593]有效的正方形-Valid Square的更多相关文章

  1. [Swift]LeetCode593. 有效的正方形 | Valid Square

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  2. [LeetCode] Valid Square 验证正方形

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  3. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  4. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  5. 593. Valid Square

    Problem statement: Given the coordinates of four points in 2D space, return whether the four points ...

  6. LC 593. Valid Square

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  7. [Swift]LeetCode221. 最大正方形 | Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

  8. 定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。(体现多态)

    实现多态的三个条件:1.要有继承2.要有抽象方法重写3.用父类指针(引用)指向子类对象 重载重写重定义的区别: 1.重载:在同一个类中进行; 编译时根据参数类型和个数决定方法调用; 子类无法重载父类; ...

  9. 最大正方形 · Maximal Square

    [抄题]: 在一个二维01矩阵中找到全为1的最大正方形 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 返回 4 [暴力解法]: 时间分析: 空间分析:i j 中保留一 ...

  10. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. Elasticsearch倒排索引结构【转载】

    一切设计都是为了提高搜索的性能 倒排索引(Inverted Index)也叫反向索引,有反向索引必有正向索引.通俗地来讲,正向索引是通过key找value,反向索引则是通过value找key. 先来回 ...

  2. 顺序结构程序设计(python)

    文章目录 1.python运算符 1.1 python算数运算 1.2python比较运算符 1.3 Python赋值运算符 1.4 python逻辑运算符 1.5 python成员运算符 1.6py ...

  3. 矩阵怪 - 2024全新矩阵产品,一键分发抖音,快手,视频号,B站,小红书!

    本方案面向谁,解决了什么问题 本方案主要面向C端客户,特别是那些在各大短视频平台(如小红书.抖音.视频号.快手.B站等)上进行内容创作和分发的个人用户.自由职业者.小型团队或企业.这些用户通常面临着在 ...

  4. UIAbility组件生命周期

    当用户打开.切换和返回到对应应用时,应用中的UIAbility实例会在其生命周期的不同状态之间转换.UIAbility类提供了一系列回调,通过这些回调可以知道当前UIAbility实例的某个状态发生改 ...

  5. games101_Homework2

    完成函数static bool insideTriangle(): 测试点是否在三角形内. 一段优雅的easy代码,没什么好说的.(但是需要修改这里传入的xy的类型为float,默认为int是想让我通 ...

  6. isPCBroswer:检测是否为PC端浏览器模式

    function isPCBroswer() { let e = navigator.userAgent.toLowerCase() , t = "ipad" == e.match ...

  7. Python框架之FastAPI

    原起: 最近项目中现存的是使用的python的异步方式进行开发, 但是只是存在脚本部分, 并没有提供对外的Web服务,正好趁机将Fastapi嵌入其中使用. 官方文档: https://fastapi ...

  8. c# 添加系统右键菜单(Windows11以前)

    今天介绍一下在注册表中添加系统右键菜单实现在文件夹的右键菜单中添加,删除以及查找是否已经添加的方法. 注意这里的方法仅限于Windows11之前使用,Windows11默认的右键菜单已经改变,需要使用 ...

  9. ZCMU-1136

    思路 一个数学问题 要知道1为奇数,2^x次方一定为偶数. 偶数=奇数+奇数,而奇数=奇数*奇数,所以x一定要是奇数才可以. 注意 没告诉范围所以要往大的方向考虑 其中1能够被任一整数整除,所以前面加 ...

  10. 一款 IDEA 必备的 JSON 处理工具插件 — Json Assistant

    Json Assistant 是基于 IntelliJ IDEs 的 JSON 工具插件,让 JSON 处理变得更轻松! 主要功能 完全支持 JSON5 JSON 窗口(多选项卡) 选项卡更名 移动至 ...