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. ovsdb-client命令

    ovsdb-server 的命令行接口. 查看有哪些数据库: ovsdb-client list-dbs [server] 查看数据库 schema: ovsdb-client get-schema ...

  2. 网络编程-I/O复用

    I/O模型 Unix下可用的I/O模型有五种: 阻塞式I/O 非阻塞式I/O I/O复用(select和poll.epoll) 信号驱动式I/O(SIGIO) 异步I/O(POSIX的aio_系列函数 ...

  3. (11)-Python3之--os模块

    1.模块介绍 os模块是路径处理模块,它提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,根据不同的平台进行相应的操作,在python编程时,经常和文件.目录打交道 ...

  4. tcpdump安装与参数详解

    Centos7安装Tcpdump 对于大部分的Linux操作系统,已经默认安装了tcpdump,可以通过以下命令查看: [root@localhost local]# tcpdump --versio ...

  5. Centos 7 Rabbitmq 安装并开机启动

    准备工作 安装wget yum install -y wget rabbitmq安装需要依赖erlang,erlang安装参考:https://www.cnblogs.com/swyy/p/11582 ...

  6. 使用nodejs构建Docker image最佳实践

    目录 简介 准备nodejs应用程序 创建Dockerfile文件 创建.dockerignore文件 创建docker image 运行docker程序 node的docker image需要注意的 ...

  7. pod资源限制和QoS探索

    简述 默认情况下,k8s不会对pod的资源使用进行限制,也就是说,pod可以无限使用主机的资源,例如CPU.内存等.为了保障k8s整体环境运行的稳定性,一般情况下,建议是对pod的资源使用进行限制,将 ...

  8. HTML 5 学习第二课

    元素:<p>+++++++++</P> 全部内容 标签:<P></P> 属性:标签内部的内容 eg:<img src=" "& ...

  9. MySql(三)存储过程和函数

    MySql(三)存储过程和函数 一.什么是存储过程和函数 二.存储过程和函数的相关操作 一.什么是存储过程和函数 存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程和函数 ...

  10. Java——break,continue,return语句

    break语句: break:用于改变程序控制流 用于do-while.while.for中时,可跳出循环而执行循环后面的语句. break的作用:终止当前循环语句的执行. break还可以用来终止s ...