Easy-题目42:223. Rectangle Area

转载 2016年05月30日 20:31:29

题目原文:
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.
题目大意:
给出(A,B),(C,D),(E,F),(G,H)四个点,如图所示求围成图形的面积。
题目分析:
在纸上多画几种情况可以发现,A>=G、B>=H、C<=E、D<=F这四种情况下,两个矩形是不重叠的,因此直接计算面积之和,否则要减去重叠部分。
源码:(language:cpp)

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int area = (C-A)*(D-B) + (G-E)*(H-F);
        if (A >= G || B >= H || C <= E || D <= F)
           return area;
        int top = min(D, H);
        int bottom = max(B, F);
        int left = max(A, E);
        int right = min(C, G);
        return area - (top-bottom)*(right-left);
    }
};

成绩:
36ms,beats 20.05%,众数36ms,37.38%.

相关文章推荐

223. Rectangle Area [easy] (Python)

题目链接https://leetcode.com/problems/rectangle-area/题目原文 Find the total area covered by two rectiline...

LeetCode #223 - Rectangle Area - Easy

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

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
  • 2232

204. Count Primes \ 223. Rectangle Area

Rectangle Area Count Primes223. Rectangle Areaclass Solution { public: int computeArea(int A, in...

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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