593. 有效的正方形

给定二维空间中四点的坐标,返回四点是否可以构造一个正方形。

一个点的坐标(x,y)由一个有两个整数的整数数组表示。

示例:

输入: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]

输出: True

注意:

所有输入整数都在 [-10000,10000] 范围内。

一个有效的正方形有四个等长的正长和四个等角(90度角)。

输入点没有顺序。

class Solution {
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
// 判断标准:向量长度相等、垂直相交、对边平行,则为正方形
// 先任意选两组进行判断,确定这两组的位置,是对角线还是对边
int[] p1p2 = vector(p1, p2);
int[] p3p4 = vector(p3, p4); // 长度
if (getLength(p1p2) == 0) {
return false;
} // p1p2和p3p4:只能是垂直相交并且相等 或者 平行
if (isVerticalAndLengthEqualVector(p1p2, p3p4)) {
// p1p2, p3p4垂直 那么p1p3和p2p4必平行
if (isParallelVector(vector(p1, p3), vector(p2, p4))) {
return true;
}
} else if (isParallelVector(p1p2, p3p4)){
// p1p2, p3p4平行, 但确定不了方向,因此下面两组任意垂直且相等即可
if (isVerticalAndLengthEqualVector(vector(p1, p4), vector(p2, p3)) ||
isVerticalAndLengthEqualVector(vector(p1, p3), vector(p2, p4))) {
return true;
}
} return false;
} private int[] vector(int[] p1, int[] p2) {
return new int[]{p1[0] - p2[0], p1[1] - p2[1]};
} private double getLength(int[] p1p2) {
return Math.sqrt(p1p2[0]*p1p2[0] + p1p2[1]*p1p2[1]);
} private boolean isParallelVector(int[] p1, int[] p2) {
return p1[0]*p2[1] == p1[1]*p2[0];
} private boolean isVerticalAndLengthEqualVector(int[] p1, int[] p2) {
return p1[0]*p2[0] + p1[1]*p2[1] == 0 && getLength(p1) == getLength(p2);
}
}

Java实现 LeetCode 593 有效的正方形(判断正方形)的更多相关文章

  1. C#版 - Leetcode 593. 有效的正方形 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. 【Hadoop离线基础总结】Hue与Hive集成

    目录 1.更改hue的配置hue.ini 2.启动hive的metastore以及hiveserver2服务 3.启动hue进程,查看Hive是否与Hue集成成功 1.更改hue的配置hue.ini ...

  2. react中dangerouslySetInnerHTML使用

    在react中,通过富文本编辑器进行操作后的内容,会保留原有的标签样式,并不能正确展示. 在显示时,将内容写入__html对象中即可.具体如下: <div dangerouslySetInner ...

  3. xpath加PHP对网站相关数据的截取

    首先了解一串代码 <?php  $url = 'http://www.baidu.com';$ch = curl_init();curl_setopt($ch, CURLOPT_FILE, fo ...

  4. threading模块—Python多线程编程

    Threading 模块 threading 模块除了提供基本的线程和锁定支持外,还提供了更高级别.功能更全面的线程管理.threading 模块支持守护线程,其工作方式是:守护线程一般是一个等待客户 ...

  5. Print输出颜色字体方法

    书写格式:     开头部分:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m      注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个:另外由于 ...

  6. 【SMB源码解析系列】——001.JumpEngine函数

    在SMB的源码中大概有不到20处看起来很奇怪的指令,它的格式是通过jsr指令调用一个名为JumpEngine的函数,其后并不是跟随某些后续的逻辑指令,而是通过.dw定义了一系列16位地址. 我们可以看 ...

  7. 下载Android代码

    1.由于墙,无法下载android源码,但是又不想利用清华源repo下载整个工程,只下载个别仓库 解决办法: 2.下载frameworks/base: git clone https://androi ...

  8. helm使用

    helm 0. helm安装 基本上到github上面,下载二进制就行.mac的话用brew安装. https://github.com/helm/helm brew: brew install ku ...

  9. django安装及其他模块导入

    django安装 python项目第三方模块配置 pip3 list------查看当前pip安装的第三方模块

  10. 06.drf(django)的权限

    默认配置已经启用权限控制 settings 'django.contrib.auth', 默认 migrate 会给每个模型赋予4个权限,如果 ORM 类不托管给django管理,而是直接在数据库中建 ...