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.

本题是一道简单的数学题,意思就是要求两个矩形的覆盖面积。

根据每个矩形是由它的下左边角和它的上右边角定义的特征,再结合公式:覆盖面积=两个矩形的面积-相交的面积,即可。

代码如下:

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 = (D>H)?H:D; //和用min(D,H)是一样的
int bottom = max(B, F);
int left = max(A, E);
int right = min(C, G);
return area - (top-bottom)*(right-left);
}
};

看了看别人做的,

class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { if(A > E) return computeArea(E, F, G, H, A, B, C, D); int res = (C - A)*(D- B) + (G - E)*(H - F); if(C > E && B < H && F < D) res -= (min(C, G) - E) * (min(D, H) - max(B, F)); return res; }
};

核心思想都是差不多的。 

  

leetcode:Rectangle Area的更多相关文章

  1. [LeetCode] 850. Rectangle Area II 矩形面积之二

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...

  2. [LeetCode] 223. Rectangle Area 矩形面积

    Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...

  3. LeetCode之“数学”:Rectangle Area

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

  4. leetcode之Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  5. Java for LeetCode 223 Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  6. (easy)LeetCode 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  7. Java [Leetcode 223]Rectangle Area

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

  8. LeetCode(41)-Rectangle Area

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

  9. leetcode 850. Rectangle Area II

    给定一些矩形2 求覆盖面积 矩形不超过200个 1 算法1 朴素思想 虽然朴素但是代码却有意思 利用容斥原理 复杂度高达 N*2^N class Solution: def intersect(rec ...

随机推荐

  1. mysql if条件

    #if表达式 SELECT reg_no, IF(reg_no='718170554','黄色宾利','红色宾利') FROM car WHERE reg_no IN ('718170554','12 ...

  2. linux下命令行查看Memcached运行状态(shell)

    stats查看memcached状态的基本命令,通过这个命令可以看到如下信息:STAT pid 22459                             进程IDSTAT uptime 10 ...

  3. Java 8怎么了之二:函数和原语

    [编者按]本文作者为专注于自然语言处理多年的 Pierre-Yves Saumont,Pierre-Yves 著有30多本主讲 Java 软件开发的书籍,自2008开始供职于 Alcatel-Luce ...

  4. CAP定理与RDBMS的ACID

    一.分布式领域CAP理论 CAP定理指在设计分布式系统时,一致性(Consistent).可用性(Availability).可靠性(分区容忍性Partition Tolerance)三个属性不可能同 ...

  5. ZOJ3724 Delivery(树状数组??)

    题意:给你一个有向图,第一类边是从第i个点到第i+1个点的,还有多出来的m条二类边,是从u到v的,同样是有向的.然后你要处理询问,从u到v经过最多一次二类边的最短距离是多少. 题目我觉得是神题,然后看 ...

  6. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

  7. 简约的单页应用引擎:sonnyJS

    点这里 SonnyJS是一个简约的单页应用引擎和HTML预处理器,旨在帮助开发人员和设计人员创建难以置信的强大和快速的单页网站. 主要特性: 模板嵌套,模板继承 动态同步模板路由(非Ajax) 跨窗口 ...

  8. 在Vista或Windows 7系统上安装Sharepoint 2007

    在Vista或Windows 7系统上安装Sharepoint 2007 2010-03-05 18:53:19|  分类: 技术文章|字号 订阅     SharePoint 2007 不能直接安装 ...

  9. ***ECharts图表入门和最佳实践

    ECharts数据图表系统? 5分钟上手! [ECharts简介] ECharts开源来自百度商业前端数据可视化团队,基于html5 Canvas,是一个纯Javascript图表库,提供直观,生动, ...

  10. 离开csdn来到blog园

    csdn里没有限制阅读访问的功能,所以我选择来到cnblog 但是不得不说,cnblog做的界面很丑,我个人很不喜欢,但是没办法