题目
题解
我的题解:
public class m223 {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int visualArea = getArea(ax1, ay1, ax2, ay2) + getArea(bx1, by1, bx2, by2);
if (ax2 < bx1 || ay1 > by2 || ax1 > bx2 || ay2 < by1) {
return visualArea;
}
int overlaps = getArea(Math.max(ax1, bx1), Math.max(ay1, by1), Math.min(ax2, bx2), Math.min(ay2, by2));
return visualArea - overlaps;
}
public int getArea(int x1, int y1, int x2, int y2) {
return (x2 - x1) * (y2 - y1);
}
}
官方题解:
class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1), area2 = (bx2 - bx1) * (by2 - by1);
int overlapWidth = Math.min(ax2, bx2) - Math.max(ax1, bx1), overlapHeight = Math.min(ay2, by2) - Math.max(ay1, by1);
int overlapArea = Math.max(overlapWidth, 0) * Math.max(overlapHeight, 0);
return area1 + area2 - overlapArea;
}
}
|