LeetCode 题解 593. Valid Square (Medium)
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)的更多相关文章
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 593. Valid Square
Problem statement: Given the coordinates of four points in 2D space, return whether the four points ...
- LC 593. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [LeetCode 题解]: Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode题解(20)--Valid Parentheses
https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode题解:Valid Palindrome(判断回文)
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- leetcode题解:Valid Parentheses(栈的应用-括号匹配)
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- LeetCode题解之Valid Triangle Number
1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...
随机推荐
- 什么是STP
简介 了解STP 配置STP 相关信息 简介 STP(Spanning Tree Protocol)是运行在交换机上的二层破环协议,环路会导致广播风暴.MAC地址表震荡等后果,STP的主要目的就是确保 ...
- 快速排序与荷兰国旗及Partition问题
快速排序与荷兰国旗及Partition问题 需求: 1.Partition过程 给定一个数组arr,和一个整数num.请把小于等于num的数放在数组的左边,大于num的数放在数组的右边. 要求额外空间 ...
- vfd-cloud——一个适合练习上手的云存储网盘springboot项目(开发中)
vfd-cloud 一个基于SpringBoot的云存储网盘项目,适合练手学习SpringBoot,用到的技术栈列到了下面.支持用户的注册登陆及修改密码,利用邮箱进行验证.支持 ...
- WebRTC 泄漏真实 IP 地址
WebRTC(网页即时通信,Web Real-Time Communication) 它允许浏览器内进行实时语音或视频对话,而无需添加额外的浏览器扩展.包括 Chrome.Firefox.Opera. ...
- 【转载】【GDB】GDB with Python
作者:薛定谔的喵链接:https://zhuanlan.zhihu.com/p/152274203来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 你还在用GDB调试程序 ...
- SumatraPDF设置护眼背景
高级选项中: 1 FixedPageUI [ 2 TextColor = #000000 3 BackgroundColor = #C7EDCC 4 SelectionColor = #f5fc0c ...
- vue组件中data为什么必须是一个函数?
因为JavaScript的特性所导致,在component中,data必须以函数的形式存在,不可以是对象. 组建中的data写成一个函数,数据以函数返回值的形式定义,这样每次复用组件的时候,都会返回一 ...
- C# 8.0 可空(Nullable)给ASP.NET Core带来的坑
Nullable reference types(可为空引用类型) 可为空引用类型不讲武德 C#8.0 引入了"可为空引用类型"和"不可为空引用类型",使我们能 ...
- 数据湖-Apache Hudi
Hudi特性 数据湖处理非结构化数据.日志数据.结构化数据 支持较快upsert/delete, 可插入索引 Table Schema 小文件管理Compaction ACID语义保证,多版本保证 并 ...
- 性能优化(CSS优化)
高质量的CSS代码体现在三个方面:可读性和可维护性和高性能.对于"前端工程师"来说如何平衡"追求高性能"和"可维护性"是很值得思考的问题. ...