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. 15.Filter(过滤器)

    1.管理所有WEB资源:(Jsp, Servlet, 静态图片文件或静态 html 文件等)文件等进行拦截,从而实现一些特殊的功能 2.Filter接口中有一个doFilter方法,当我们编写好Fil ...

  2. sql 存储过程笔记3

    16:22 2014/1/26一.定义变量--简单赋值declare @a int set @a = 5 print @a --使用select语句赋值declare @user1 nvarchar( ...

  3. 屏蔽恶意IP

    #!/bin/bash cat /var/log/secure | grep Failed | awk -F " " '{print $11}'| sort| uniq -c| a ...

  4. bat 获取当前目录的父目录

    bat 获取当前目录的父目录 @echo off echo batchfile=%0 echo full=%~f0 setlocal for %%d in (%~dp0.) do set Direct ...

  5. cubemx+stm32串口学习汇总资料

    这篇文章是串口中断的文章--STM32基于CubeMX的高速串口收发程序(中断模式)比较有帮助. http://www.stmcu.org.cn/module/forum/thread-616613- ...

  6. 【Android-自定义控件】SwipeRefreshDemo 下拉刷新,上拉加载

    参考:https://github.com/PingerOne/SwipeRefreshDemo 谷歌官方的SwipeRefreshLayout控件,只有下拉刷新功能. 自定义的SwipeRefres ...

  7. Non-standard serial port baud rate setting

    ////combuad_recv.cpp #include <stdio.h> /*标准输入输出定义*/ #include <stdlib.h> /*标准函数库定义*/ #in ...

  8. 使用ice-plugin-fusion给icedesign的模板加个主题(theme)

    最近一直处于半失业状态,好多年没有更新对前端的理解了,闲来看看前端技术的发展.因为一直是个草台班子,身兼多职东看看西看看,一直没太搞清楚iceworks.ant design, fusion等等一堆阿 ...

  9. Postman实现数字签名,Session依赖, 接口依赖, 异步接口结果轮询

    Script(JS)为Postman赋予无限可能 基于Postman 6.1.4 Mac Native版 演示结合user_api_demo实现 PS 最近接到任务, 要把几种基本下单接口调试和持续集 ...

  10. 用sql语句查询一列名中的各个数值的个数

    SELECT COUNT(case when f.fileState=2 then 0 end) as fixed,COUNT(case when f.fileState=3 then 0 end) ...