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:

  1. All the input integers are in the range [-10000, 10000].
  2. A valid square has four equal sides with positive length and four equal angles (90-degree angles).
  3. 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的更多相关文章

  1. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  2. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  3. LC 593. Valid Square

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  4. [LeetCode] Valid Square 验证正方形

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  5. [Swift]LeetCode593. 有效的正方形 | Valid Square

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  6. C#版 - Leetcode 593. 有效的正方形 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. leetcode 学习心得 (3)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 517. Super Washing Machines You have n super washing ...

随机推荐

  1. Zygote和System进程的启动过程、Android应用进程启动过程

    1.基本过程 init脚本的启动Zygote Zygote进程的启动 System进程的启动 Android应用进程启动过程 2.init脚本的启动 +------------+ +-------+ ...

  2. Application,Service,Activity 三者的Context的应用场景

    Application 的 context 不是万能的,所以也不能随便乱用,对于有些地方则必须使用 Activity 的 Context, 对于Application,Service,Activity ...

  3. C# 判断文件和文件夹是否存在并创建

    C# 判断文件和文件夹是否存在并创建 using System; using System.Data; using System.Configuration; using System.Collect ...

  4. Python variable 作用域和初始化

    Python 根据LEGB rule在不同的namespace中找变量 在def的函数中对global 变量做修改还是不推荐的,应该将其作为参数传入函数 try: do_something() cnt ...

  5. OSW

    OSWatcher 工具 下载文档 :Metalink Doc ID 301137.1 Oswatcher 主要用于监控主机资源,如CPU,内存,网络以及私有网络等.其中私有网络需要单独配置. 需要说 ...

  6. hihocoder offer收割编程练习赛11 C 岛屿3

    思路: 并查集的应用. 实现: #include <iostream> #include <cstdio> using namespace std; ][]; int n, x ...

  7. Ajax请求WebService跨域问题

    1.背景 用Jquery中Ajax方式在asp.net开发环境中WebService接口的调用 2.出现的问题 原因分析:浏览器同源策略的影响(即JavaScript或Cookie只能访问同域下的内容 ...

  8. 对称加密DES加密

    DES加密: des是对称加密,加密和解密需要相同的秘钥,它的密码最长56位,必须是8的倍数,秘钥越长,越安全. package com.trm.util.encrypt; import java.s ...

  9. react link引入外部css样式的坑

    刚开始的代码是这样的,使用react router4.x写的demo路由跳转后,页面的没有渲染,是因为没有引入外部css文件(或者说引入外部文件路径错误) <!DOCTYPE html> ...

  10. vue之Render函数

    (1)什么是Render函数 先来看一个场景,在博客网中,一般有一级标题.二级标题.三级标题...,为了方便分享url,它们都做成了锚点,点击后,会将内容加载网址后面,以#分隔. 例如‘特性’是一个& ...