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.

这道题给了我们四个点,让验证这四个点是否能组成一个正方形,刚开始博主考虑的方法是想判断四个角是否是直角,但即便四个角都是直角,也不能说明一定就是正方形,还有可能是矩形。还得判断各边是否相等。其实这里可以仅通过边的关系的来判断是否是正方形,根据初中几何的知识可以知道正方形的四条边相等,两条对角线相等,满足这两个条件的四边形一定是正方形。那么这样就好办了,只需要对四个点,两两之间算距离,如果计算出某两个点之间距离为0,说明两点重合了,直接返回 false,如果不为0,那么就建立距离和其出现次数之间的映射,最后如果我们只得到了两个不同的距离长度,那么就说明是正方形了,参见代码如下:

解法一:

class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
unordered_map<int, int> m;
vector<vector<int>> v{p1, p2, p3, p4};
for (int i = ; i < ; ++i) {
for (int j = i + ; j < ; ++j) {
int x1 = v[i][], y1 = v[i][], x2 = v[j][], y2 = v[j][];
int dist = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
if (dist == ) return false;
++m[dist];
}
}
return m.size() == ;
}
};

我们其实不用建立映射,直接用个集合 HashSet 来放距离就行了,如果最后集合中不存在0,且里面只有两个数的时候,说明是正方形,参见代码如下:

解法二:

class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
unordered_set<int> s{d(p1, p2), d(p1, p3), d(p1, p4), d(p2, p3), d(p2, p4), d(p3, p4)};
return !s.count() && s.size() == ;
}
int d(vector<int>& p1, vector<int>& p2) {
return (p1[] - p2[]) * (p1[] - p2[]) + (p1[] - p2[]) * (p1[] - p2[]);
}
};

参考资料:

https://leetcode.com/problems/valid-square/

https://leetcode.com/problems/valid-square/discuss/103442/c-3-lines-unordered_set

[LeetCode] Valid Square 验证正方形的更多相关文章

  1. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. [LeetCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  3. [LeetCode] Valid Number 验证数字

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  4. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  5. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. [LeetCode] Valid Anagram 验证变位词

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  7. [Leetcode] valid palindrome 验证回文

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

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

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

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

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

随机推荐

  1. supervisor进程管理工具的使用

    supervisor是一款进程管理工具,当想让应用随着开机启动,或者在应用崩溃之后自启动的时候,supervisor就派上了用场. 广泛应用于服务器中,用于引导控制程序的启动 安装好superviso ...

  2. C语言中的atan和atan2

    本文内容为转载,是在阅读 RTKLIB源码时意识到的这个问题,原文地址为:https://www.cnblogs.com/dutlei/archive/2013/01/14/2860332.html ...

  3. beta冲刺 用户使用调查报告

    测评结果 一.使用体验 数据加载响应很快,页面切换丝滑流畅. UI有点偏暗,有些字被覆盖了. 页面布局过于居中,两侧空白范围较大. 总体功能完善. 二.登录.注册.忘记密码界面 管理员登录按钮太靠下, ...

  4. Beta预备会议

    1. 讨论组长是否重选的议题和结论. 我们小组决定组长更换为林洋洋同学,他Web开发经验比较丰富,对任务的分配会更加明确,由于上一阶段中存在进度偏慢的问题,我们希望在Beta阶段通过更好的分工安排来保 ...

  5. 展示博客(Beta版本)

    团队:xjbz 1. 团队成员博客,源码仓库地址. coding:https://git.coding.net/z404395979/xjbz.git 钟平辉(071):http://www.cnbl ...

  6. Linux挂载

    1 文件系统中相关目录 dev:设备文件 media:挂载媒体设备,如光驱,U盘 mnt:让用户临时挂载别的文件系统 2 磁盘分区相关知识 1)磁盘包括IDE和SCSI两种接口: IDE接口:速度慢但 ...

  7. 201621123068 作业07-Java GUI编程

    1. 本周学习总结 1.1 思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 注册.事件.事件源.监听 1.2 任意编写事件处理相 ...

  8. 关于第一次使用vue-cli

    前段时间终于终于可以用vue-cli,webpack做个企业站,记一下过程... 首先node.js,按照vue官网的步骤命令提示符走一波,网速原因,所以用的是淘宝镜像 cnpm # 全局安装 vue ...

  9. 【iOS】swift 保持代码优美的10个方法

    这篇Swift风格指南与你看到的其他的指南有所不同,此篇指南主要焦点集中在打印和Web展示的可读写上.我们创建此篇风格指南的目的,是为了让我们的图书.教程以及初学者套件中的代码保持优美和一致,即使我们 ...

  10. 第一篇:Python入门

    一.编程与编程语言 编程的目的: 计算机的发明,是为了用机器取代/解放人力,而编程的目的则是将人类的思想流程按照某种能够被计算机识别表达方式传递给计算机,从而达到让计算机能够像人脑/电脑一样自动执行的 ...