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 ...
随机推荐
- 写了shell脚本想一键启动三台虚拟机的Zookeeper,却不知道为啥总是启动不了
首先,一键启动的shell脚本是这样的 #! /bin/bash case $1 in "start"){ for i in node01 node02 node03 do ssh ...
- 【HBase】HBase和Hue的整合
目录 一.修改hue.ini配置文件 二.启动HBase的thrift server服务 三.启动Hue 四.页面访问 一.修改hue.ini配置文件 cd /export/servers/hue-3 ...
- ubuntu 1604升级到ubuntu 1804无法忽视的细节问题(亲测有效)
升级ubuntu系统,遇到很多问题,可能你在升级的时候也会碰到,希望对你有所帮助: 文章目录 1 常规升级过程 2 更改过源 3 无法全部更新 4 其他的问题 5 升级成功 6 无法进入gnome 6 ...
- 数学建模(二)优劣解距离法Topsis模型部分
步骤: (一)统一指标类型:指标正向化(转化为极大型)(论文) 越大越好极大型指标,效益型指标 越小越好极小型指标,成本型指标 max-x,max=max{xi} 落在某个区间[a,b]是最好的,区间 ...
- [hdu5215]无向图找奇偶环
题意:如标题 思路:对于奇环,一个二分图判定就ok了,有奇环<=>非二分图.对于偶环,考虑环必定出现在双联通分量里面,可以先求出图的双联通分量,对于一个双联通分量,对于双联通分量里面的每个 ...
- 安卓APP承载网页(WebView)
安卓APP自身如何打开网页,如何制作一个简单的浏览器,WebView在其中将是一个重要的角色.WebView是一个基于WebKit引擎.展现Web页面的控件. Webview 是一个基于webkit引 ...
- Crash-fix-2:org.springframework.http.converter.HttpMessageNotReadableException
最近开始对APP上的Crash进行对应,发现有好多常见的问题,同一个问题在多个APP都类似的出现了,这里记录下这些常见的错误. crash Log: org.springframework.http. ...
- keepalived的一些。。
继续采坑..有些坑,踩了才知道. 1.文件位置不能错. 首先是安装, 源码编译,--prefix=/usr/local/keepalive 然后用 sbin/keepalive -f ...conf ...
- scrapy实现数据持久化、数据库连接、图片文件下载及settings.py配置
数据持久化的两种方式:(1)基于终端指令的持久化存储:(2)基于管道的持久化存储 基于终端指令的持久化存储 在爬虫文件的parse方法中必须要return可迭代对象类型(通常为列表或字典等)的返回值, ...
- day06:三级菜单练习0218
#1:省份数列:data = { "北京":{ "昌平":{ "沙河":["oldboy","电信" ...