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:

  1. All the input integers are in the range [-10000, 10000].
  2. A valid square has four equal sides with positive length and four equal angles (90-degree angles).
  3. 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的更多相关文章

  1. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  2. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  3. 593. Valid Square

    Problem statement: Given the coordinates of four points in 2D space, return whether the four points ...

  4. [LeetCode] Valid Square 验证正方形

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  5. [Swift]LeetCode593. 有效的正方形 | Valid Square

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  6. [LC] 367. Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  7. 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 ...

  8. LC 20 Valid Parentheses

    问题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  9. [LC] 125. Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. 解释mysql 语句

    一.在我们创建mysql数据库的时候我们经常会用到这句SQL: CREATE DATABASE TEST DEFAULT CHARACTER SET utf8 COLLATE utf8_general ...

  2. 操作xml文件

    http://www.cnblogs.com/ 一.xml文件体系如下: <?xml version="1.0" encoding="utf-8" ?&g ...

  3. 《数字图像处理(MATLAB)》冈萨雷斯

    <数字图像处理(MATLAB)>冈萨雷斯 未完结! 参考:数字图像处理——https://blog.csdn.net/dujing2019/article/category/8820151 ...

  4. Airflow安装错误:sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError)

    1 完整的异常信息: raise errorclass, errorvalue sqlalchemy.exc.OperationalError: (_mysql_exceptions.Operatio ...

  5. Jmeter官方插件实现Dubbo接口测试

    目前主流的分布式框架有Dubbo和SpringCloud, SpringCloud是基于Http协议的分布式框架,Dubbo是基于RPC的分布式框架,Jmeter没有内置对Dubbo接口的支持,很难直 ...

  6. LoadRunner(7)

    一.参数化策略 1.Select next row(How? 如何取?)取值方式 选择下一行 1)Sequential:顺序的 每个VU都从第一行开始,顺序依次向下取值: 数据取完可以从头循环重复使用 ...

  7. 通过自动回复机器人学Mybatis---加强版

    第2章 接口式编程 介绍 Mybatis 的接口式编程,并说明为什么要采用这种形式,以及 Mybatis 是如何实现的

  8. PHP配置文件(php.ini)详解

    [PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而php.ini的设置更改可以反映出相当的变化, ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;;;;; ...

  9. python中函数的参数传递小结

    “”“ 函数的参数 --必须参数,默认参数,组合参数 --函数我作为参数 --对象作为参数 --*args  可变参数 --**kwargs关键字参数 “”” def function1(a,b,*a ...

  10. WebService如何封装XML请求 以及解析接口返回的XML

    原 WebService如何封装XML请求 以及解析接口返回的XML 置顶 2019年08月16日 15:00:47 童子泛舟 阅读数 28 标签: XML解析WebService第三方API 更多 ...