391. 完美矩形
通过面积 和顶点的特征,间接确定矩形是唯一的
class Solution {
public:
bool isRectangleCover(vector<vector<int>>& rectangles) {
// 顶点法+ 总面积法
long sumErea = 0;
unordered_set<string> st;
int x1=INT_MAX,y1=INT_MAX,x2=INT_MIN,y2=INT_MIN ;
//
for(auto &e : rectangles)
{
int curx1 = e[0];
int cury1 = e[1];
int curx2 = e[2];
int cury2 = e[3];
x1 = min(x1,curx1);
y1 = min(y1,cury1);
x2 = max(x2,curx2);
y2 = max(y2,cury2);
sumErea += (long)(curx2-curx1)*(long)(cury2-cury1);
vector<string> tPoints = {
to_string(curx1)+','+to_string(cury1),
to_string(curx1)+','+to_string(cury2),
to_string(curx2)+','+to_string(cury1),
to_string(curx2)+','+to_string(cury2)
};
for(auto &ne : tPoints)
{
if(st.count(ne))
{
st.erase(ne);
}else{
st.insert(ne);
}
}
}
long allErae = (long)(x2-x1)*(long)(y2-y1);
vector<string> topPoints = {
to_string(x1)+','+to_string(y1),
to_string(x1)+','+to_string(y2),
to_string(x2)+','+to_string(y1),
to_string(x2)+','+to_string(y2)
};
for(auto &e : topPoints)
{
if(!st.count(e))
{
return false;
}
}
return allErae == sumErea && st.size() == 4;
}
};