【LeetCode】 223. Rectangle Area

来源:互联网 发布:随机算法软件 编辑:程序博客网 时间:2024/05/23 11:14

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.

Rectangle Area

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

public class Solution {    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {        int left = Math.max(A, E);        int right = Math.max(Math.min(C, G), left);        int down = Math.max(B, F);        int up = Math.max(Math.min(D, H), down);        return (C - A) * (D - B) + (G - E) * (H - F) - (right - left) * (up - down);    }}


0 0