LeetCode-223.Rectangle Area

原创 2016年05月30日 15:16:08

https://leetcode.com/problems/rectangle-area/

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.

一开始用了愚蠢的办法,用了各种判断

参考后(http://www.cnblogs.com/sunshisonghit/p/4580069.html)得到下面代码

public int ComputeArea(int A, int B, int C, int D, int E, int F, int G, int H) 
    {
        int areaAB = (C - A) * (D - B) + (G - E) * (H - F);
        int left = Math.Max(A, E);
        int right = Math.Min(C, G);
        int top = Math.Min(D, H);
        int down = Math.Max(B, F);
        if (left < right && down < top)
            return areaAB - (right - left) * (top - down);
        return areaAB;
    }


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

相关文章推荐

Leetcode 223 Rectangle Area 矩形区域面积

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

【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
  • 2222

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...

LeetCode-223. Rectangle Area (JAVA)求矩形覆盖面积

LeetCode-223. Rectangle Area (JAVA)求矩形覆盖面积

[leetcode] 223.Rectangle Area

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

Leetcode_223_Rectangle Area

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46868363 Find the total area...

LeetCode #223 - Rectangle Area - Easy

ProblemFind the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is def...

Rectangle Area - LeetCode 223

题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is d...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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