223. 矩形面积
思路(来源https://mp.weixin.qq.com/s/gypmkZFX87O1KzMt7XW0DA):
class Solution {
public:
int area(int x,int y)
{
return x*y;
}
int area(int x1,int y1,int x2,int y2)
{
return area(x2-x1,y2-y1);
}
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int cross_x=max(0,min(ax2,bx2)-max(ax1,bx1));
int cross_y=max(0,min(ay2,by2)-max(ay1,by1));
return area(ax1,ay1,ax2,ay2)+area(bx1,by1,bx2,by2)-area(cross_x,cross_y);
}
};