LC 593. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a square.
The coordinate (x,y) of a point is represented by an integer array with two integers.
Example:
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.
从边入手,如果四个点不重合,那么形成的四条边加两个对角线只可能有两个情况,且对角线要长。
Runtime: 16 ms, faster than 57.63% of Java online submissions for Valid Square.
class Solution {
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
int[][] p = {{p1[],p1[]},{p2[],p2[]},{p3[],p3[]},{p4[],p4[]}};
int cnt = ;
int[] len = new int[];
for(int i=; i<; i++){
for(int j=i+; j< ;j++){
if(p[i][] == p[j][] && p[i][] == p[j][]) return false;
len[cnt++] = (p[i][]-p[j][])*(p[i][]-p[j][])+(p[i][]-p[j][])*(p[i][]-p[j][]);
}
}
Arrays.sort(len);
if(len[] == len[] && len[] == len[] && len[] > len[]){
return true;
}
return false;
}
}
LC 593. Valid Square的更多相关文章
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- 【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 ...
- [LeetCode] Valid Square 验证正方形
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [Swift]LeetCode593. 有效的正方形 | Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [LC] 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- LC 794. Valid Tic-Tac-Toe State
A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...
- LC 20 Valid Parentheses
问题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- [LC] 125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- 8.Spring整合Hibernate_2_声明式的事务管理(Annotation的方式)
声明式的事务管理(AOP的主要用途之一) (Annotation的方式) 1.加入annotation.xsd 2.加入txManager bean 3.<tx:annotation-drive ...
- CI/CD持续集成小结
一.概念 什么是devops,基于Gitlab从零开始搭建自己的持续集成流水线(Pipeline) https://blog.csdn.net/chengzi_comm/article/details ...
- 二、MySQL介绍
目录 一.MySQL背景 二.MySQL的优点 三.MySQL安装 四.MySQL服务的启动和停止 五.MySQL登录和退出 六.MySQL常用命令 (一)常用命令 (二)语法规范 (三)SQL语言细 ...
- linux入门常用指令2.安装nginx
下载nginx包 nginx-1.10.3.tar.gz 解压 [root@localhost src]# tar -zxvf nginx-1.10.3.tar.gz [root@localhost ...
- JS中call()和apply()以及bind()的区别
一.方法定义: apply:调用一个对象的一个方法,用另一个对象替换当前对象.例如:B.apply(A, arguments);即A对象应用B对象的方法. call:调用一个对象的一个方法,用另一个对 ...
- CodeForcs 1169B Good Triple
CodeForcs 1169B Good Triple 题目链接:http://codeforces.com/problemset/problem/1169/B 题目描述:给你m对不超过n的数字,找出 ...
- python之命令行可以做的事儿
前言 说起python,大家都知道可以做很多有趣的事儿.知乎.CSDN上很多大牛做过很多或高大上或实用的事儿.参见这篇文章Python趣味代码整合之提升学生编程兴趣, 另外,我发现命令行也可以做很多事 ...
- 编译原理实战——使用Lex/Flex进行编写一个有一定词汇量的词法分析器
编译原理实战--使用Lex/Flex进行编写一个有一定词汇量的词法分析器 by steve yu 2019.9.30 参考文档:1.https://blog.csdn.net/mist14/artic ...
- JDBC连接数据库报错:Loading class `com.mysql.jdbc.Driver'. This is deprecated.
使用JDBC连接数据库时出现报错, 报错内容:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver cla ...
- Ubuntu系统---安装思维导图XMind
Ubuntu系统---安装思维导图XMind @wp20181011 XMind安装很简单,这里简单记录记录一下.分为三步:1,下载.2,安装.3,设置桌面快捷图标. 详细步骤如下: 1,下载. 首先 ...