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. DoNet:浅淡对delegate的理解

    1 前言 C#的相关文档,MSDN上其实已经很详细了,关于delegate的使用可以参 考MSDN上的文档https://msdn.microsoft.com/zh-cn/library/900fyy ...

  2. [uva_la7146 Defeat the Enemy(2014 shanghai onsite)]贪心

    题意:我方n个军队和敌方m个军队进行一对一的对战,每个军队都有一个攻击力和防御力,只要攻击力不小于对方就可以将对方摧毁.问在能完全摧毁敌方的基础上最多能有多少军队不被摧毁. 思路:按防御力从大到小考虑 ...

  3. 给出两个单词(start和end)与一个字典,找出从start到end的最短转换序列

    问题 给出两个单词(start和end)与一个字典,找出从start到end的最短转换序列.规则如下: 一次只能改变一个字母 中间单词必须在字典里存在 例如: 给出 start = "hit ...

  4. lsof 命令用法:查看已删除空间却没有释放的进程

    查看已经删除的文件,空间有没有释放,没有的话kill掉pid lsof -n |grep deleted lsof简介lsof(list open files)是一个列出当前系统打开文件的工具. 问题 ...

  5. Linux中链接的概念

    一,软链接 touch f1 创建符号链接,两个文件inode不同 ln -s f1 f3 二,硬链接 touch f1 创建硬链接, 两个文件inode相同 ln  f1 f2 硬链接和软链接,最大 ...

  6. 自动配置的Springboot内junit测试单元不能运行

    解决测试单元不能运行 问题:测试单元的 @Test 前面没有运行图标 解决 IDEA内:File - Setting - Plugins:搜到JUnitGenerator2.0,安装,重启IDEA 光 ...

  7. vue学习-第三个DEMO(计算属性和监视) v-model基础用法

    <div id="demo"> 姓:<input type="text" placeholder="First Name" ...

  8. 网鼎杯2020青龙组writeup-web

    本文首发于Leon的Blog,如需转载请注明原创地址并联系作者 AreUSerialz 开题即送源码: <?php include("flag.php"); highligh ...

  9. scrapy五大核心组件

    scrapy五大核心组件 引擎(Scrapy)用来处理整个系统的数据流处理, 触发事务(框架核心) 调度器(Scheduler)用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. ...

  10. Storm-jdbc-2讲 高级API及Trident

    之前对Storm集成JDBC写了一个简单的demo,最近深度研究了下,代码如下 首先,先写一个抽象类,便于减少代码的重复性: import com.google.common.collect.List ...