223. Rectangle Area

Total Accepted: 30441
Total Submissions: 104452
Difficulty: Easy

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Assume that the total area is never beyond the maximum possible value of int.

Java:

1
2
3
4
5
6
7
8
public class Solution {
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int r = (C - A) * (D - B) + (G - E) * (H - F);
        if (!(A >= G || B >= H || C <= E || D <= F))
            r -= (Math.min(C, G) - Math.max(A, E)) * (Math.min(D, H) - Math.max(B, F));
        return r;
    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。