593. Valid Square
Problem statement:
Given the coordinates of four points in 2D space, return whether the four points could construct a square.
The coordinate (x,y) of a point is represented by an integer array with two integers.
Example:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True
Note:
- All the input integers are in the range [-10000, 10000].
- A valid square has four equal sides with positive length and four equal angles (90-degree angles).
- Input points have no order.
Solution one: a set to find the relative of four points(AC).
As the second problem in this contest, the key points are to find the right order of these four points and check the if one/two/three/fours points are overlapped. It will be very easier if we know the relative positions of these four points. The answer comes out to check the length of four sides and two diagonals.
I use the default sorting characteristic of a set instead of designing an algorithm to find their relative positions.
The final order after sorting by a set is 0, 1, 3, 2 in counterclockwise.
The efficiency does not matter in this problem, correctness is on the top.
class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
vector<vector<int>> points(, vector<int>(, ));
set<vector<int>> points_set;
points_set.insert(p1);
points_set.insert(p2);
points_set.insert(p3);
points_set.insert(p4);
if(points_set.size() != ){
return false;
}
int idx = ;
for(auto point : points_set){
points[idx] = point;
idx++;
}
if( dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])){
return true;
}
return false;
}
private:
int dis_square(vector<int> p1, vector<int> p2){
return (p1[] - p2[]) * (p1[] - p2[]) + (p1[] - p2[]) * (p1[] - p2[]);
}
};
Solution two: STL sort algorithm(AC). This is concise and easy to understand(Better).
Another good alternative approach to find the relative position of these four points are the sort algorithm in STL.
The default sorting algorithm sort the first element and sort the second element. The sorted order is still 0, 1, 3, 2 in counterclockwise.
But, we need to check if one or more positions are overlapped before sorting.
class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
// check if one or more points are overlapped
if(p1 == p2 || p1 == p3 || p1 == p4 || p2 == p3 || p2 == p4 || p3 == p4){
return false;
}
vector<vector<int>> points = {p1, p2, p3, p4};
sort(points.begin(), points.end());
if(dis_square(points[], points[]) == dis_square(points[], points[]) // check four sides
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
&& dis_square(points[], points[]) == dis_square(points[], points[])
// check the diagonals
&& dis_square(points[], points[]) == dis_square(points[], points[])){
return true;
}
return false;
}
private:
int dis_square(vector<int> p1, vector<int> p2){
return (p1[] - p2[]) * (p1[] - p2[]) + (p1[] - p2[]) * (p1[] - p2[]);
}
};
593. Valid Square的更多相关文章
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- LC 593. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [LeetCode] Valid Square 验证正方形
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- [Swift]LeetCode593. 有效的正方形 | Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- C#版 - Leetcode 593. 有效的正方形 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode 学习心得 (3)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 517. Super Washing Machines You have n super washing ...
随机推荐
- linux安装glassfish并布署
1 https://glassfish.java.net/download.html 2 准备工作:需要jdk7以上版本 Java EE 7 requires JDK 7 (or above) 下载g ...
- C语言中Static和Const关键字的的作用 -- 转
static作用:“改变生命周期” 或者 “改变作用域” 程序的局部变量存在于(堆栈)中,全局变量存在于(静态区 )中,动态申请数据存在于( 堆)中. 1.作用于变量: 用static声明局部变量-- ...
- Laravel环境搭建
在有了初步认知后,当然就要开始在自己的电脑上搭建Laravel的开发环境了. 系统环境需求 PHP 5.3.7或者更高版本,如果没有系统没有安装PHP环境的,请到下面地址下载:http://cn2.p ...
- 新浪qq登录
一:到腾讯QQ互联上申请APPID和APPKEY.申请地址: http://connect.qq.com/ 如同,这里我们可以获取到需要跳转到的APPID和APPKEY.新浪微博的申请同理 二:在Th ...
- windows server 2008 r2 IIS7下网站配置 只允许指定的IP地址访问
步骤一.找到ip地址和域限制 步骤二.添加全部拒绝 步骤三.添加允许访问的ip地址(局域网填写局域网ip,公网填写公网ip) 步骤四:如果想要拒绝某些ip访问,直接在规则中添加拒绝条目就可以
- 【数据分析 R语言实战】学习笔记 第四章 数据的图形描述
4.1 R绘图概述 以下两个函数,可以分别展示二维,三维图形的示例: >demo(graphics) >demo(persp) R提供了多种绘图相关的命令,可分成三类: 高级绘图命令:在图 ...
- R in action读书笔记(12)第九章 方差分析
第九章方差分析 9.2 ANOVA 模型拟合 9.2.1 aov()函数 aov(formula, data = NULL, projections =FALSE, qr = TRUE, contra ...
- leetcode_951. Flip Equivalent Binary Trees_二叉树遍历
https://leetcode.com/problems/flip-equivalent-binary-trees/ 判断两棵二叉树是否等价:若两棵二叉树可以通过任意次的交换任意节点的左右子树变为相 ...
- C# 设置系统环境变量
using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; ...
- sh NonUniqueObjectException
话题引入: 使用hibernate进行更新操作时,首先调用了findById方法获取要修改的对象,此时session没有被关闭,接着重新创建一个对象,将要修改的属性值赋值给这个对象.调用修改方法抛出如 ...