LeetCode 题解 593. Valid Square (Medium)

判断给定的四个点,是否可以组成一个正方形



https://leetcode.com/problems/valid-square/solution/

bug

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-11-11
* @modified
*
* @description 593. Valid Square
* @difficulty Medium
* @complexity O(n)
* @augments
* @example
* @link https://leetcode.com/problems/valid-square/
* @link https://leetcodee-cn.com/problems/valid-square/
* @link https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/565/week-2-november-8th-november-14th/3527/
* @solutions
*
*/ const log = console.log; /**
* @param {number[]} p1
* @param {number[]} p2
* @param {number[]} p3
* @param {number[]} p4
* @return {boolean}
*/
var validSquare = function(p1, p2, p3, p4) {
const args = arguments;
// log(`args`, args);
let result = false;
// math
const minValues = [];
const maxValues = [];
const absValues = [];
for (let i = 0; i < args.length; i++) {
const [
min,
max,
] = args[i];
minValues.push(Math.abs(min));
maxValues.push(Math.abs(max));
// minValues.push(min);
// maxValues.push(max);
absValues.push(Math.abs(Math.abs(max) - Math.abs(min)));
}
const min = minValues.sort((a, b) => a > b ? 1 : -1)[0];
const max = maxValues.sort((a, b) => a > b ? -1 : 1)[0];
const sum = absValues.reduce((acc, item) => acc += item, 0);
// log(`min =`, min)
// log(`max =`, max)
// log(`abs =`, absValues)
// log(`sum / 2 === max - min`, sum / 2, (max - min));
// log(`sum === 2 * (max - min)`, sum , 2 * (max - min));
if(sum === 2 * (max - min)) {
// if((sum / 2) === (max - min)) {
result = true;
}
return result;
}; // const p1 = [0, 0], p2 = [1, 1], p3 = [1, 0], p4 = [0, 1];
// const p1 = [0, 0], p2 = [50, 100], p3 = [100, 50], p4 = [100, 100];
// const p1 = [50, 50], p2 = [50, 100], p3 = [100, 50], p4 = [100, 100];
// const p1 = [25, 25], p2 = [75, 25], p3 = [25, 75], p4 = [75, 75]; const p1 = [5, -3], p2 = [7, -3], p3 = [6, -2], p4 = [6, -4]; // const p1 = [6987,-473], p2 = [6985,-473], p3 = [6986,-472], p4 = [6986,-474]; const test = validSquare(p1, p2, p3, p4); log(`test =`, test) /* // 5 / 1
min = 0
max = 1
abs = [ 0, 0, 1, 1 ] // 100 / 50
min = 0
max = 100
abs = [ 0, 50, 50, 0 ] // 50 / 50
min = 50
max = 100
abs = [ 0, 50, 50, 0 ] // 50 / 50
min = 25
max = 75
abs = [ 0, 50, 50, 0 ] */ /* Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True Note: All the input integers are in the range [-10000, 10000].
A valid square has four equal sides with positive length and four equal angles (90-degree angles).
Input points have no order. */

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


LeetCode 题解 593. Valid Square (Medium)的更多相关文章

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

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

  2. 593. Valid Square

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

  3. LC 593. Valid Square

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

  4. [LeetCode 题解]: Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. LeetCode题解(20)--Valid Parentheses

    https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...

  6. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. leetcode题解:Valid Palindrome(判断回文)

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  8. leetcode题解:Valid Parentheses(栈的应用-括号匹配)

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  9. LeetCode题解之Valid Triangle Number

    1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...

随机推荐

  1. Scrapy———反爬蟲的一些基本應對方法

    1. IP地址驗證 背景:有些網站會使用IP地址驗證進行反爬蟲處理,檢查客戶端的IP地址,若同一個IP地址頻繁訪問,則會判斷該客戶端是爬蟲程序. 解決方案: 1. 讓Scrapy不斷隨機更換代理服務器 ...

  2. 4、剑指offer——从尾到头打印链表java实现

    **题目描述** **输入一个链表,按链表从尾到头的顺序返回一个ArrayList.** 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M 思路:   1.如果链 ...

  3. spring 之7种重要设计模式

    Spring中涉及的设计模式总结 1.简单工厂(非23种设计模式中的一种) 实现方式: BeanFactory.Spring中的BeanFactory就是简单工厂模式的体现,根据传入一个唯一的标识来获 ...

  4. Makefile 描述的是文件编译的相关规则,它的规则主要是两个部分组成,分别是依赖的关系和执行的命令 PHONY伪目标实践

    Makefile的工作流程 http://c.biancheng.net/view/7091.html Makefile文件是什么? 我们教程主要是讲的是 Makefile .很多 Linux(Uni ...

  5. Flash 终将谢幕:微软将于年底( 2020 年 )停止对 Flash 的支持

    近日,微软宣布将于今年 12 月终止对 Adobe Flash Player 的支持,届时,微软旗下所有浏览器都将无法使用 Flash,Adobe 也不会在今年 12 月后发布安全更新.早在 2017 ...

  6. Python学习【第4篇】:元组魔法

    template = "i am {name},age:{age}" v = template.format(**{"name":'xiaoxing',&quo ...

  7. HMS Core华为分析丨受众细分,多场景促进精益运营

    用户的偏好不同,对产品的需求也不一样,要想更好地培养用户粘性,就需要因人施策,精细化运营,而受众细分是精细化运营的重要方法之一.受众细分是根据用户属性和行为数据,将具有相同或类似特征的用户归为一个群组 ...

  8. 数据分析中常用的Excel函数

    数据分析中excel是一个常见且基础的数据分析工具,要想掌握好它,学会使用其中的常用函数是一个绕不过去的坎.从网上搜集的资料来说,基本上确定了数据分析中Excel的常用函数有以下这六类 数学函数:SU ...

  9. HTML5的表单input元素的新属性

    知识点 <HTML5的表单input元素的新属性>,留待学习分享... <!-- HTML5的表单input元素的新属性 Autocomplete:自动完成功能 Autofocus: ...

  10. Grafana+Prometheus通过node_exporter监控Linux服务器信息

    Grafana+Prometheus通过node_exporter监控Linux服务器信息 一.Grafana+Prometheus通过node_exporter监控Linux服务器信息 1.1nod ...