C++11 solution using unordered_map


  • 0
    W
    class Solution {
    public:
        bool isReflected(vector<pair<int, int>>& points) {
            if (points.size() < 2) return true;
            int maxVal = std::numeric_limits<int>::min();
            int minVal = std::numeric_limits<int>::max();
            unordered_map<int, unordered_set<int>> index;
            for(const auto& p : points) {
                minVal = min(minVal, p.first);
                maxVal = max(maxVal, p.first);
                auto it = index.find(p.first);
                if (it == index.end()) {
                    index[p.first] = {p.second};
                } else {
                    it->second.insert(p.second);
                }
            }
            int doubleMidVal = minVal + maxVal;
            for (auto it = index.begin(); it != index.end(); ++it) {
                int coVal = doubleMidVal - it->first;
                auto coIt = index.find(coVal);
                if (coIt == index.end() || coIt->second != it->second ) {
                    return false;
                }
            }
            return true;
        }
    };

Log in to reply
 

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