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. D. Mahmoud and a Dictionary 种类并查集

    http://codeforces.com/contest/766/problem/D 所谓种类并查集,题型一般如下:给定一些基本信息给你,然后又给出一些信息,要求你判断是真是假.例如给出a和b支持不 ...

  2. CF940D Alena And The Heater

    思路: 模拟. 实现: #include <bits/stdc++.h> using namespace std; const int INF = 1e9; ], n; string b; ...

  3. js插件定义事件中,this引用的实现模拟

    在web项目中,经常会使用jquery和mui等js框架,之前只是按照API说明去按规则使用,比如在jq和mui中,事件处理函数中可以直接用this访问事件源,如下面的代码: <!DOCTYPE ...

  4. git ---回到过去

    git命令回顾 git checkout /git reset -git reset HEAD~    //~代表回滚到第几个版本.. 有多个的话可以在~后面加个数字 git reset --mixe ...

  5. Java多线程编程核心技术---Lock的基本概念和使用

    Lock接口: ReentrantLock的基本功能: ReentrantLock的lock和unlock方法进行加锁,解锁.可以起到和synchronized关键字一样的效果: 选择性通知!!!: ...

  6. C#斐波那契数列递归算法

    public static int Foo(int i)        {            if (i < 3)            {                return 1; ...

  7. js添加千位分隔符

    function thousandBitSeparator(num){ var re=/\d{1,3}(?=(\d{3})+$)/g; var n1=num.toString().replace(/^ ...

  8. ALTER USER - 改变数据库用户帐号

    SYNOPSIS ALTER USER name [ [ WITH ] option [ ... ] ] where option can be: [ ENCRYPTED | UNENCRYPTED ...

  9. MySQL 中去重 distinct 用法

    在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count( ...

  10. JS的type类型为 text/template

    JS标签中有时候会看见<script type="text/tmplate" >,大概就是一个放置模板的地方,而这些东西并不显示在页面 在js里面,经常需要使用js往页 ...