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. OSI网络通信工作流程的标准化 ----- 理论

    OSI 七层模型 1 应用层 [提供用户服务,具体功能由特定的程序而定] 2 表示层 [数据的压缩优化,加密] 3 会话层 [建立应用级的连接,选择传输服务] 4 传输层 [提供不同的传输服务,流量控 ...

  2. 英语是学习Java编程的基础吗

    就当前市场行情需求来看,Java人才需求依旧火爆,在如今互联网时代,手机移动端的软件开发是非常重要的,如今无论是大中小企业都是需要进行软件的开发的,又因为Java是开源的使用起来可以节约一大批的成本, ...

  3. idea启动卡死,项目界面一直processing

    1 原因 因为上次退出项目,非正常退出,导致索引生成有问题. 2 解决办法 删除项目根目录下 .idea文件夹,然后重新打开,重新indexing生成索引文件

  4. Python 获取 exe 的 icon 并且保存

    Python 获取 exe 的 icon 并且保存 参考链接:https://mail.python.org/pipermail/python-win32/2009-April/009078.html ...

  5. iOS native plugin 的代码sample

    https://bitbucket.org/Unity-Technologies/iosnativecodesamples/src/stable/ 不在这个stable版本 在2017 dev这个版本

  6. fsLayui缓存使用

    概述 缓存主要使用在编辑或查看页面查询数据的方式,通过后端servlet接口获取数据还是通过前端缓存获取.缓存可以使用在实时性要求不高的上面.减少后端servlet请求. 使用步骤 配置支持缓存 需要 ...

  7. Java笔记(基础第一篇)

    一.初识java 1.Java是一种可以编写跨平台的.面向对象的程序设计语言. Java开发分成以下3个方向: (1). java SE:主要用于桌面程序的开发.是java EE和java ME的基础 ...

  8. mongodb的安装与使用(一)

    一.什么是MongoDB ? MongoDB一种由C++语言编写的,是一个基于分布式文件存储的非关系型数据库(NoSql),是一种强大.灵活.可扩展的数据存储方式,因为MongoDB是文档模型,数据结 ...

  9. maven工程指定jdk版本,maven全局配置jdk的版本

  10. 桥接模式(Bridge)---结构型

    1 基础知识 定义:将抽象部分与它的具体实现部分分离,使得它们都可以独立变化.特征:通过组合的方式建立两个之间的联系而不是继承. 使用场景:抽象和具体实现之间增加更多的灵活性:一个类存在两个(多个)独 ...