leetcode No593. Valid Square
Posted Dufre.WC
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode No593. Valid Square相关的知识,希望对你有一定的参考价值。
Question
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:
- All the input integers are in the range [-10000, 10000].
- A valid square has four equal sides with positive length and four equal angles (90-degree angles).
- Input points have no order.
Algorithm
4个点,两两相连,一共是6条边,如果是正方形,则6条边的分为两类:
- 四个相等的边
- 两个相等的对角线
用<key, value>
的结构,也就是map,存储<边长,个数>
的关系,如果map的size是两个,则满足以上条件。
另外注意边长等于0的case。
Code
class Solution
public:
int distance(vector<int> p1, vector<int> p2)
return (p1[1] - p2[1])*(p1[1] - p2[1]) + (p1[0] - p2[0])*(p1[0] - p2[0]);
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4)
unordered_map<int, int> hash;
int x1 = distance(p1, p2); hash[x1]++;
int x2 = distance(p1, p3); hash[x2]++;
int x3 = distance(p1, p4); hash[x3]++;
int x4 = distance(p2, p3); hash[x4]++;
int x5 = distance(p2, p4); hash[x5]++;
int x6 = distance(p3, p4); hash[x6]++;
if (x1==0 || x2==0 || x3==0 || x4==0 || x5==0 || x6==0 || hash.size()!=2)
return false;
return true;
;
开发者涨薪指南
48位大咖的思考法则、工作方式、逻辑体系
以上是关于leetcode No593. Valid Square的主要内容,如果未能解决你的问题,请参考以下文章