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. 微信公众号支付JSAPI网页,total_fee错误不正确,header重定向参数丢失,无法获取订单号和金额解决

    微信公众号支付官方demo错误, 公众号支付只能用在微信里,也就是微信内部浏览器. 1.到WxPayHubHelper.php文件 JsApi_pub()类下createOauthUrlForCode ...

  2. C语言第一次博客作业——输入输出格式

    一.PTA实验作业 注意:本次PTA实验共有8个题目,在博客上只要贴:4个题目就可以,分别为: 题目1:7-3 温度转换 (1分) 题目2:7-4 将x的平方赋值给y (2分) 题目3:7-6 是不是 ...

  3. alpha-咸鱼冲刺day9

    一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 大概差不多了.不过提交似乎又出了问题正在修改ing 四,问题困难 页面整合啥的凑合一下.因为后面跟着学长速成的PHP,现在还在研 ...

  4. 从0开始的LeetCode生活—9. Palindrome Number(回文数)

    题目大意: 判断输入的数字是不是回文数.所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字.负数不会是回文数. 解题思路: 思路一:如果这个数是负数,则返回false,否则用一 ...

  5. 2018年3月份的PTA(一)

    写程序证明p++等价于(p)++还是等价于(p++)? 由程序说明p++等价于(p)++,因为(p++)在程序中是没有地址的,而输出中p++和(p)++的地址不同是由于在线C语言开发环境地址是动态的 ...

  6. C语言——第0次作业(二)

    1.翻阅邹欣老师的关于师生关系博客,并回答下列问题,每个问题的答案不少于500字: 1.最理想的师生关系是健身教练和学员的关系,在这种师生关系中你期望获得来自老师的哪些帮助? 在现代软件工程讲义 0 ...

  7. raid5 / raid5e / raid5ee的性能对比及其数据恢复原理

    RAID 5 是一种存储性能.数据安全和存储成本兼顾的存储解决方案. RAID 5可以理解为是RAID 0和RAID 1的折中方案.RAID 5可以为系统提供数据安全保障,但保障程度要比Mirror低 ...

  8. 第二章 JavaScript核心语法

    第二章   avaScript核心语法 一.变量的声明和赋值 JavaScript是一种弱类型语言,没有明确的数据类型,也就是在声明变量时不需要指定数据类型,变量的类型由赋给变量的值决定. 在Java ...

  9. js控制表格实时编辑

    点击添加,在表格的最后一行添加一行表单元素,右侧按钮变为保存和取消.(点击保存,数据用ajax无刷新添加到界面,点击取消,取消此行的添加.)点击编辑,在本行改为表单,带有原来的值,右侧按钮变为确认和取 ...

  10. 解决编写的 html 乱码问题