class Solution {
public:
bool isRectangleCover(vector<vector<int>>& rectangles) {
unordered_set<string> s;
int x1=INT_MAX;
int y1=INT_MAX;
int x2=INT_MIN;
int y2=INT_MIN;
int area=0;
for(vector<int> v:rectangles)
{
x1=min(x1,v[0]);
y1=min(y1,v[1]);
x2=max(x2,v[2]);
y2=max(y2,v[3]);
area+=(v[3]-v[1])*(v[2]-v[0]);
string p1=to_string(v[0])+" "+to_string(v[1]);
string p2=to_string(v[2])+" "+to_string(v[1]);
string p3=to_string(v[0])+" "+to_string(v[3]);
string p4=to_string(v[2])+" "+to_string(v[3]);
if(s.find(p1)!=s.end())
s.erase(p1);
else
s.insert(p1);
if(s.find(p2)!=s.end())
s.erase(p2);
else
s.insert(p2);
if(s.find(p3)!=s.end())
s.erase(p3);
else
s.insert(p3);
if(s.find(p4)!=s.end())
s.erase(p4);
else
s.insert(p4);
}
string p1=to_string(x1)+" "+to_string(y1);
string p2=to_string(x1)+" "+to_string(y2);
string p3=to_string(x2)+" "+to_string(y1);
string p4=to_string(x2)+" "+to_string(y2);
if(s.find(p1)==s.end()||s.find(p2)==s.end()||s.find(p3)==s.end()||s.find(p4)==s.end()||s.size()!=4)
return false;
else
return area==(y2-y1)*(x2-x1);
}
};
391. Perfect Rectangle
最新推荐文章于 2021-11-16 12:37:18 发布