[leetcode]593. Valid Square
来源:互联网 发布:塞上牛羊空许约 知乎 编辑:程序博客网 时间:2024/06/05 08:03
题目链接:https://leetcode.com/problems/valid-square/#/description
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
思路:
If we calculate all distances between 4 points, 4 smaller distances should be equal (sides), and 2 larger distances should be equal too (diagonals). As an optimization, we can compare squares of the distances, so we do not have to deal with the square root and precision loss.
Therefore, our set will only contain 2 unique distances in case of square (beware of the zero distance though).
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(0) && s.size()==2; } int d(vector<int>& p1,vector<int>& p2) { return pow(p1[0]-p2[0],2)+pow(p1[1]-p2[1],2); }};
阅读全文
0 0
- [leetcode]593. Valid Square
- leetcode 593. Valid Square
- Leetcode 593. Valid Square
- leetcode 593. Valid Square 正方形的判定
- LeetCode:Valid Perfect Square
- 【Leetcode】valid perfect square
- [leetcode] Valid Perfect Square
- leetcode---Valid Perfect Square
- LeetCode: Valid Word Square
- LeetCode -- Valid Perfect Square
- LEETCODE--Valid Perfect Square
- LeetCode : Valid Perfect Square
- leetcode 593: Valid Square
- leetcode:valid square
- LeetCode 367. Valid Perfect Square
- Leetcode 367. Valid Perfect Square
- [leetcode] 367. Valid Perfect Square
- [leetcode]367. Valid Perfect Square
- HDU1255-覆盖的面积(线段树+扫描线——面积交)
- 关于gcd的函数和过程两种不同写法
- 装饰者模式(待记录)
- 在web开发中hibernate3插入数据库日期不一致的问题解决方法
- pycharm调试debug入门
- [leetcode]593. Valid Square
- 工具安装随笔
- 工厂模式
- Ajax系列面试题总结02
- LINUX摄像驱动二:虚拟驱动VIVI测试及彻底分析
- 智联招聘爬虫
- sublime text3 常用快捷键
- (165)深度表达式
- 25--- HTTPD(apache)