题解方法
容斥原理
- 两矩阵在水平和竖直方向上的交集投影线段构成交集面积的长和宽
- 交集投影线段均大于0时,才说明有交集面积
核心代码
1 2 3
| int overlapWidth = Math.min(ax2, bx2) - Math.max(ax1, bx1); int overlapHeight = Math.min(ay2, by2) - Math.max(ay1, by1); int overlapArea = Math.max(overlapWidth, 0) * Math.max(overlapHeight, 0);
|
题目来源
223. 矩形面积 - 力扣(LeetCode) (leetcode-cn.com)