LeetCode Rectangle Area

原创 2015年07月10日 11:17:35

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;
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode 223 Rectangle Area 矩形区域面积

计算两个矩形所在2D空间内覆盖的区域面积。

[Leetcode]-Rectangle Area

//题目:Find the total area covered by two rectilinear rectangles in a 2D plane. //Each rectangle is d...

【LeetCode-面试算法经典-Java实现】【223-Rectangle Area(矩形区域)】

【223-Rectangle Area(矩形区域)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao】原题...

LeetCode 223 Rectangle Area(矩形面积)

翻译找到在二维平面中两个相交矩形的总面积。每个矩形都定义了其左下角和右上角的坐标。(矩形如下图)假设,总占地面积永远不会超过int的最大值。原文分析这题前天试过,写了一堆判断,终究还是无果……贴几个别...
  • NoMasp
  • NoMasp
  • 2016-01-27 12:17
  • 2211

LeetCode Rectangle Area

LeetCode Rectangle Area题目思路刚开始自己写别提WA多少遍了; 后来看到标达真的被惊讶到了; 代码可以这么美;代码自己的#define min(A, B) (A > B ? ...

Leetcode 223 - Rectangle Area(矩形面积交)

题意两个和坐标轴平行的矩形,可能相交,求这两个矩形的总面积。思路关键是确定相交矩形的面积,我们可以通过确定四个顶点,也可以通过长和宽。算法1通过确定四个顶点(实际上是4条边的位置,最上面的边,最下面的...
  • Lzedo
  • Lzedo
  • 2017-03-06 11:00
  • 107

【leetcode】223. Rectangle Area

一、题目解读 Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is...

Rectangle Area - LeetCode 223

题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is d...

[Leetcode] 223. Rectangle Area 解题报告

题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle ...

LeetCode 223. Rectangle Area(两个矩形的复合面积计算)

原题网址:https://leetcode.com/problems/rectangle-area/ Find the total area covered by two rectilinear...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)