容斥原理
A∪B = A + B - A∩B
class Solution {
public:
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int width = min(ax2, bx2) - max(ax1, bx1);
int height = min(ay2, by2) - max(ay1, by1);
return (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1) - max(width, 0) * max(height, 0);
}
};
原文:https://www.cnblogs.com/xulizs666/p/15358005.html