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. 5.SpringMVC 配置式开发-处理器适配器

    处理器适配器HandlerAdapter 1.SimpleControllerHandlerAdapter(默认) 所有实现了 Controller 接口的处理器 Bean,均是通过SimpleCon ...

  2. linux之getopts

    在编写shell脚本中,经常要处理一些输入参数,在使用过程中发现getopts更加方便,能够很好的处理用户输入的参数和参数值. getopts用于处理用户输入参数,举例说明使用方法: while ge ...

  3. scp上传文件到远程服务器

    scp -P 22 E:/download/2028792_www.yeves.cn_nginx/cloud.pem root@ip:/usr/local/src

  4. 我理解的epoll(一)——实现分析

    epoll项目中用了几次,但是对于其原理只是一知半解.我希望通过几篇blog能加深对她的理解. 我认为epoll是同步IO,因为他在调用epoll_wait时,内核在有I/O就绪前是阻塞的,虽然可以将 ...

  5. AJAX—JSON和Django内置序列化

    JSON 什么是JSON JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 * J ...

  6. 5.安装bacula-web(监控页面)

    1.   安装bacula-web(监控页面) 用途:监控bacula状态. http://docs.bacula-web.org/en/master/index.html bacula-web-7. ...

  7. 认识和学习bash

    认识Bash这个Shell 查看linux下shells: [shichaogeng@study etc]$ vim /etc/shells 查看登入时取得到的shell: [shichaogeng@ ...

  8. web开发:javascript基础

    一.js引入 二.变量的定义 三.三种弹出框 四.调试方式 五.数据类型 六.数据类型转换 七.运算符 八.分支机构 九.循环结构 十.异常处理 十一.函数 一.js引入 - ES: ECMAScri ...

  9. dao层取值用List<map<String,Object>>接收有序map

    发现一个好玩的Map, 当需要Map有序时用java.util.LinkedHashMap接收,是有序map resultType="java.util.LinkedHashMap" ...

  10. mybatis的配置文件详解(二)

    一.properties 这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递.例如 1) <?xml versio ...