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. Vue基础之插值表达式的另一种用法!附加变量的监听!

    Vue基础之插值表达式的另一种用法!附加变量的监听! 讲这个之前我们先回顾一下原来的用法! <body> <!-- Vue.js的应用可以分为两个重要的组成部分 一个是视图! 另一个 ...

  2. 中文电子病历命名实体识别(CNER)研究进展

    中文电子病历命名实体识别(CNER)研究进展 中文电子病历命名实体识别(Chinese Clinical Named Entity Recognition, Chinese-CNER)任务目标是从给定 ...

  3. .NET, NETCORE 怎么写 "超时"代码,解析"超时"代码原理!

    干货:本人不会长篇大论.能贴上去的,就是干货,能用一两句话讲明白的,不会大讲概念,不会浪费大家宝贵的时间. 前言:我们发现,超时是个非常重要的概念,如果在通讯架构中,没有超时的设计,那么这个通讯架构就 ...

  4. https://nginx.org/en/docs/http/request_processing.html

    https://nginx.org/en/docs/http/request_processing.html

  5. HTML Standard系列:Event loop、requestIdleCallback 和 requestAnimationFrame

    HTML Standard系列:Event loop.requestIdleCallback 和 requestAnimationFrame - 掘金 https://juejin.im/post/5 ...

  6. 前序遍历 排序 二叉搜索树 递归函数的数学定义 return 递归函数不能定义为内联函数 f(x0)由f(f(x0))决定

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  7. Jenkins部署web项目到Tomcat(热部署)

    使用这个方式的话需要tomcat中有初始时Manage这个项目,本质上是通过http://ip:port/manager/html这个地址的上传接口进行上传,进行热部署(需要远程tomcat 必须开启 ...

  8. WPF显示命名空间不存在对应名称

    3个办法 1 切换到Release模式,再生成.生成成功后切换回Debug模式就不报错了.这是Release模式下找不到我们自定义的控件导致的报错.所以切换为Release后生成则可以解决此问题. 2 ...

  9. JavaScript代码是怎么执行的?

    前言 众所周知,JavaScript是单线程语言.所以JavaScript是按顺序执行的! 先编译再执行 变量提升 请看下面的例子: console.log(cat) catName("Ch ...

  10. 不要在nodejs中阻塞event loop

    目录 简介 event loop和worker pool event loop和worker pool中的queue 阻塞event loop event loop的时间复杂度 Event Loop中 ...