[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);    }};

原创粉丝点击