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. (12)-Python3之--openpyxl模块

    1.安装 pip install openpyxl 2.Excel操作的流程 1.打开excel,进入工作薄 workbook2.选择表单 Sheet 3.单元格  Cell4.读写操作 3.Exce ...

  2. Shell从入门到精通

    熟悉基本shell操作不仅是运维的基本功,对于开发来说也是多多益善,我在学习的过程中,总结了十个练手的小demo,并附上涉及的知识点,仅供娱乐. 1. 多线程ping监控,检查同一网段的IP是否连通 ...

  3. try-catch 异常捕获学习

    #include <iostream> #include <string> #include <vector> #include <stdexcept> ...

  4. .axios的特点有哪些

    从浏览器中创建XMLHttpRequests:node.js创建http请求:支持Promise API:拦截请求和响应:转换请求数据和响应数据:取消请求:自动换成json.axios中的发送字段的参 ...

  5. forEach、for in、for of三者区别

    forEach更多的用来遍历数组for in 一般常用来遍历对象或jsonfor of数组对象都可以遍历,遍历对象需要通过和Object.keys()for in循环出的是key,for of循环出的 ...

  6. XV6学习(8)中断和设备驱动

    驱动是操作系统中用于管理特定设备的代码:驱动控制设备硬件,通知硬件执行操作,处理中断,与等待该设备IO的进程进行交互. 当设备需要与操作系统进行交互时,就会产生中断(陷阱的一种),之后内核的陷阱处理代 ...

  7. 动态传参,命名空间,嵌套,gloabal,nonlocal

    一.动态传参 动态接受位置参数:  *参数名 def eat(*food): print(food) #多个参数传递进去,收到的内容是元祖tuple eat("盖浇饭", &quo ...

  8. 日志框架(Log4J、SLF4J、Logback)--日志规范与实践

    文章目录 一.Log4j 1.1新建一个Java工程,导入Log4j包,pom文件中对应的配置代码如下: 1.2resources目录下创建log4j.properties文件. 1.3输出日志 1. ...

  9. 使用C#实现数据结构堆

    一. 堆的介绍: 堆是用来排序的,通常是一个可以被看做一棵树的数组对象.堆满足已下特性: 1. 堆中某个节点的值总是不大于或不小于其父节点的值 任意节点的值小于(或大于)它的所有后裔,所以最小元(或最 ...

  10. 用鸿蒙开发AI应用(七)触摸屏控制LED

    [小年答谢,新春送礼]免费抽取1000元京东卡+更多新春好礼~查看详情>>> 目录:前言背景知识编译用户程序框架子系统基于AbilityKit开发的Ability总结 前言上一篇,我 ...