Java实现 LeetCode 593 有效的正方形(判断正方形)
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 有效的正方形(判断正方形)的更多相关文章
- C#版 - Leetcode 593. 有效的正方形 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 深入理解JS中的对象(一)
目录 一切皆是对象吗? 对象 原型与原型链 构造函数 参考 1.一切皆是对象吗? 首先,"在 JavaScript 中,一切皆是对象"这种表述是不完全正确的. JavaScript ...
- git使用-忽略文件更新的几种方法
有几种情况我们不希望本地文件在 git 里面得到更新. 一.情况:始终不需要git跟踪本地的一些文件 方法:使用.gitignore文件忽略 解释: 使用git init操作创建git控制管理之后,默 ...
- 老板:kill -9 的原理都不知道就敢去线上执行?明天不用来了!
GitHub 14.5k Star 的Java工程师成神之路,开放阅读了! 相信很多程序员对于Linux系统都不陌生,即使自己的日常开发机器不是Linux,那么线上服务器也大部分都是的,所以,掌握常用 ...
- ArrayList扩容机制实探
ArrayList初始化 问题:执行以下代码后,这个list的列表大小(size)和容量(capacity)分别是多大? List<String> list = new ArrayList ...
- Quartus II 与modelsim连接不上的问题
在Quartus II 中tools>options>General>EDA Tool Options 设置modelsim 路径 说明:不管是Quartus II 与modelsi ...
- [hdu2119]二分图最小覆盖,最大匹配
题意:给一个01矩阵,每次可以选一行或一列,打掉上面所有的1,求打掉所有的1所需的最小次数. 思路:经典的模型了,二分图最小覆盖=最大匹配.所谓最小覆盖是指选最少的点关联所有的边.容易得到将行和列看成 ...
- [hdu5215]无向图找奇偶环
题意:如标题 思路:对于奇环,一个二分图判定就ok了,有奇环<=>非二分图.对于偶环,考虑环必定出现在双联通分量里面,可以先求出图的双联通分量,对于一个双联通分量,对于双联通分量里面的每个 ...
- 解决:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure(真实有效)
数据库连接失败 一.例如我在SpringBoot项目中使用了阿里的数据库连接池Driud. 有次在启动的时候,会报这样的错: Caused by: com.mysql.cj.exceptions.CJ ...
- Luogu P3389 高斯消元
https://www.luogu.com.cn/problem/P3389 主元消元法[模板] 高斯消元是解决多元线性方程组的方法,再学习它之前,先引入一个东西--行列式 行列式的性质: 这里我们只 ...
- Android 开发技术周报 Issue#280
新闻 6分钟完整视频提前看光谷歌新机Pixel 4a 统一推送官方解读:消灭Android毒瘤.待机续航猛增43% Google Play细化搜索结果:可按评分.编辑推荐.最新上线过滤 教程 了解一下 ...