zoukankan      html  css  js  c++  java
  • 593. Valid Square

    Problem statement:

    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.

    Solution one: a set to find the relative of four points(AC).

    As the second problem in this contest, the key points are to find the right order of these four points and check the if one/two/three/fours points are overlapped. It will be very easier if we know the relative positions of these four points. The answer comes out to check the length of four sides and two diagonals.

    I use the default sorting characteristic of a set instead of designing an algorithm to find their relative positions.  

    The final order after sorting by a set is 0, 1, 3, 2 in counterclockwise.

    The efficiency does not matter in this problem, correctness is on the top.

    class Solution {
    public:
        bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
            vector<vector<int>> points(4, vector<int>(2, 0));
            set<vector<int>> points_set;
            points_set.insert(p1);
            points_set.insert(p2);
            points_set.insert(p3);
            points_set.insert(p4);
            if(points_set.size() != 4){
                return false;
            }
            int idx = 0;
            for(auto point : points_set){
                points[idx] = point;
                idx++;
            }
            if(    dis_square(points[0], points[1]) == dis_square(points[1], points[3]) 
                && dis_square(points[1], points[3]) == dis_square(points[3], points[2]) 
                && dis_square(points[3], points[2]) == dis_square(points[2], points[0]) 
                && dis_square(points[2], points[0]) == dis_square(points[0], points[1]) 
                && dis_square(points[0], points[3]) == dis_square(points[1], points[2])){
                return true;   
            }
            return false;
        }
    private:
        int dis_square(vector<int> p1, vector<int> p2){
            return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
        }
    };

    Solution two: STL sort algorithm(AC). This is concise and easy to understand(Better).

    Another good alternative approach to find the relative position of these four points are the sort algorithm in STL.

    The default sorting algorithm sort the first element and sort the second element. The sorted order is still 0, 1, 3, 2 in counterclockwise. 

    But, we need to check if one or more positions are overlapped before sorting.

    class Solution {
    public:
        bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
            // check if one or more points are overlapped
            if(p1 == p2 || p1 == p3 || p1 == p4 || p2 == p3 || p2 == p4 || p3 == p4){
                return false;
            }
            vector<vector<int>> points = {p1, p2, p3, p4};
            sort(points.begin(), points.end());
            if(dis_square(points[0], points[1]) == dis_square(points[1], points[3]) // check four sides
            && dis_square(points[1], points[3]) == dis_square(points[3], points[2])
            && dis_square(points[3], points[2]) == dis_square(points[2], points[0])
            && dis_square(points[2], points[0]) == dis_square(points[0], points[1])
            // check the diagonals
            && dis_square(points[0], points[3]) == dis_square(points[1], points[2])){
                return true;
            }
            return false;
        }
    private:
        int dis_square(vector<int> p1, vector<int> p2){
            return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
        }
    };
  • 相关阅读:
    高并发、高性能、高可用
    性能优化 = 改改代码?
    高级开发必须理解的Java中SPI机制
    java性能优化的50个细节(珍藏版)
    Java API 设计清单
    QString与QByteArray互相转换的方法
    QT 托盘 hover事件捕捉
    qt捕获全局windows消息
    前端开发树形插件带来的烦恼(一)
    靠谱的div引入任何外链内容
  • 原文地址:https://www.cnblogs.com/wdw828/p/6887446.html
Copyright © 2011-2022 走看看