Concise O(n) C++ Solution


  • 3
    P
    struct hashFunc {
        size_t operator()(const pair<int, int> &point) const {
            return hash<int>()(point.first) ^ (hash<int>()(point.second) << 1) >> 1;
        }
    };
    
    class Solution {
    public:
        bool isReflected(vector<pair<int, int>>& points) {
            if (points.size() <= 1) return true;
            unordered_set<pair<int, int>, hashFunc> points_set;
            int xmin = numeric_limits<int>::max();
            int xmax = numeric_limits<int>::min();
            for (const auto point : points) {
                xmin = min(xmin, point.first);
                xmax = max(xmax, point.first);
                points_set.emplace(point);
            }
            for (const auto point : points_set) {
                if (!points_set.count(make_pair(xmax + xmin - point.first, point.second)))
                    return false;
            }
            return true;
        }
    };

  • 0
    H

    So by your hash function (1,1) and (0,32) have the same hash value?


  • 0
    P

    @hanning Just modified the hash function to mitigate the collision situation.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.