关闭

LeetCode Rectangle Area

标签: javaleetcode
104人阅读 评论(0) 收藏 举报
分类:

Description:

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.

Solution:

分别求面积,然后减去重合部分的即可。

public class Solution {
    public int computeArea(int A, int B, int C, int D, int E, int F, int G,
			int H) {
		return (C - A) * (D - B) + (G - E) * (H - F) - cover(A, C, E, G)
				* cover(B, D, F, H);
	}

	int cover(int a, int b, int c, int d) {
		if (a > c) {
			int temp;

			temp = a;
			a = c;
			c = temp;

			temp = b;
			b = d;
			d = temp;
		}
		if (c >= b)
			return 0;
		if (d <= b)
			return d - c;
		return b - c;
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:62161次
    • 积分:3258
    • 等级:
    • 排名:第10440名
    • 原创:288篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    最新评论