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. C# TcpListener的编程要点

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...

  2. matrix_world_final_2011

    C  http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98613#problem/C 题意:输入16进制的n*m矩阵,其在二进制表示下有6种 ...

  3. ffmpeg iOS 编译

    编译模拟器版本1 到https://github.com/yuvi/gas-preprocessor下载gas-preprocessor.p并拷贝到/usr/sbin目录中2 下载ffmpeg源码.h ...

  4. 编程计算并输出1~n之间所有素数之和

    http://www.tuicool.com/articles/qaaA3i   TODO

  5. 本地搭建Dubbo监控中心的安装步骤

    Dubbo监控中心的安装步骤 参考链接:http://blog.csdn.net/lichunan/article/details/40349645 一.从github上下载dubbo源码进行编译: ...

  6. poj 2348

    Euclid's Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7418   Accepted: 3022 Des ...

  7. DevExpress licenses.licx 问题

    在DevExpress ( 当然并不范指DevExpress,很多收费软件都是这样的)中,licenses.licx 是用户许可证书文件,当我们使用某些ActiveX(是Microsoft对于一系列策 ...

  8. java 反射创建对象并传入参数

    /* * 通过反射创建带参数的对象 */ public Object Creatobject(String ClassPath, Object[] Params) throws Exception { ...

  9. Win7 _Object_header 中的 TypeIndex

    Win7 比较 xp下ObjectHeader中的内容有所变化,xp直接在OBJECT_HEADER里保存了POBJECT_TYPE指针,而Win7中把所有的对象类型放在了一个表里,这个表叫做ObTy ...

  10. java编译做了哪些事?

    Javac编译器,主要做了如下的事情:1.解析与填充符号表:    2.注解处理器:    3.语义分析与字节码生成:        3.1.标注检查        3.2.数据及控制流分析      ...