C++ solution for Line Reflection


  • 0
    G
    class Solution {
    private:
        static bool comp(pair<int,int> a, pair<int,int> b){
            return a.first<b.first;
        }
    public:
        bool isReflected(vector<pair<int, int>>& points) {
            if(points.size()==0)return true;
            map<pair<int,int>,int> mp;
            sort(points.begin(),points.end(),comp);
            for(int i=0;i<points.size();i++){
                mp[points[i]]++;
            }
            int xmin=points[0].first;
            int xmax=points[points.size()-1].first;
            double yaxis=(double)(xmin+xmax)/2.0;
            for(int i=0;i<points.size();i++){
                double a=(yaxis-(double)points[i].first)*2.0+(double)points[i].first;
                int xa=(int)a;
                pair<int,int> p=make_pair(xa,points[i].second);
                if(!(mp[points[i]]&&mp[p]))return false;
            }
            return true;
            
        }
    };

Log in to reply
 

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