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 ...
随机推荐
- Ubuntu 1804 安装xmind8详细过程
安装比较简单, 折腾了很久,一启动就报错,切换了JDK版本就能用了: 安装 登陆官网,下载xmind8: 下载得到文件xmind-8-update9-linux.zip: 将文件解压至路径xmind下 ...
- 如何在MATLAB下把模糊推理系统转化为查询表(转载)
如何在MATLAB下把模糊推理系统转化为查询表(原创) http://foundy.blog.163.com/blog/static/2633834420090212202156/?mode=edit ...
- ocelot jwt 进行统一验证
前一个帖子发了有关jwt 验证api的内容,这一次将jwt集成到ocelot网关中. ocelot集成jwt有一个很不错的nuget包,ocelot.jwtauthorize ,但是这个包似乎支持n ...
- 使用JDBC操作MySQL
使用JDBC操作MySQL 步骤 加载驱动 连接数据库 操作数据库(增删改查) 关闭结果集,操作,数据库 准备工作 java连接MySQL的jar包 加载数据库驱动 public class Load ...
- 正则表达式 [:graph:] 含义
[:graph:] 代表printable and visible的字符,是除空格符(空格键与[TAB]键)之外的所有按键, 控制字符不算[:graph:] https://www.regular-e ...
- SQL server 的使用中的subString() 和 charIndex() 实现筛选 某个字段的部分数据
subString(): SUBSTRING ( expression , start , length ) 参数expression 是字符串.二进制字符串.text.image.列或包含列的表达式 ...
- java-> 分包分层
项目分层(分包)的作用 程序为什么要分包分层? 以顾客去饭店吃饭案例分析一下: 小饭店: 一个服务员搞定(接待顾客\点菜\炒菜) 大饭店: 迎宾员(是否有预定\ 询问吃中餐还是西餐或者烧烤等\ 几位用 ...
- ES6-10笔记(一)
大纲 (慕课的图先用着) scope-作用域 作用域是指程序源代码中定义变量的区域,规定了如何查找变量,也就是确定当前执行代码对变量的访问权限. JavaScript作用域共有两种主要的工作模型--词 ...
- 八个开源的 Spring Boot 前后端分离项目,一定要收藏!
八个开源的 Spring Boot 前后端分离项目 最近前后端分离已经在慢慢走进各公司的技术栈,不少公司都已经切换到这个技术栈上面了.即使贵司目前没有切换到这个技术栈上面,我们也非常建议大家学习一下前 ...
- MySQL++:Liunx - MySQL 主从复制
目标:搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,实现主从复制 环境:虚拟机 主数据库:192.168.211.101 从数据库:192.168.211.102 MySQL 安装可参 ...