题目链接

  题目要求:

  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.

  Credits:
  Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.

  为了更好说明问题,在这里特地画了几个图:

  

  我们要求的面积 = 两个矩形面积和 - 两者重叠部分。

  

  可能出现的四种情况。

  根据以上四种可能出现的情况,我们编写程序如下(参考自一博文):

 class Solution {
public:
int overlapArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int v1 = max(A, E);
int v2 = min(C, G);
int v = v2 - v1; int h1 = max(B, F);
int h2 = min(D, H);
int h= h2 - h1; if(v <= || h <= )
return ;
else
return v * h;
} int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int area1 = (C - A) * (D - B);
int area2 = (G - E) * (H - F);
int overlapRegion = overlapArea(A, B, C, D, E, F, G, H);
return area1 + area2 - overlapRegion;
}
};

LeetCode之“数学”:Rectangle Area的更多相关文章

  1. 【刷题-LeetCode】223. Rectangle Area

    Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...

  2. 【一天一道LeetCode】#223. Rectangle Area

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Find th ...

  3. 【LeetCode】223. Rectangle Area 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...

  4. 【LeetCode】223 - Rectangle Area

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

  5. LeetCode OJ 223.Rectangle Area

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

  6. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

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

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

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

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

  9. LeetCode 223. 矩形面积(Rectangle Area)

    223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...

  10. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

随机推荐

  1. FORM开发之键性弹性域开发

    1.创建表时带有键弹性域字段 SUMMARY_FLAG VARCHAR2(1) , /* 必须有此字段 */ ENABLED_FLAG VARCHAR2(1) , /* 必须有此字段 */ START ...

  2. AR模块常用函数

    --AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...

  3. System startup files

    System startup files When you log in, the shell defines your user environment after reading the init ...

  4. 【Netty源码分析】数据读取过程

    首先客户端连接到服务端时服务端会开启一个线程,不断的监听客户端的操作.

  5. PyCharm 开发Django ,错误汇总

    近期略微接触了一下Django.在学习的过程中可谓是坎坎坷坷,遇到了很多的问题. 下面就来谈一谈我对Django的一点点的见解. Django项目的创建 使用PyCharm来开发Django项目是非常 ...

  6. UNIX网络编程——利用ARP和ICMP协议解释ping命令

    一.MTU 以太网和IEEE 802.3对数据帧的长度都有限制,其最大值分别是1500和1492字节,将这个限制称作最大传输单元(MTU,Maximum Transmission Unit)      ...

  7. 最简单的基于DirectShow的示例:视频播放器

    ===================================================== 最简单的基于DirectShow的示例文章列表: 最简单的基于DirectShow的示例:视 ...

  8. (一〇六)iPad开发之UIPopoverController的使用

    很多App里都有一种点击显示的悬浮气泡菜单,例如下图: 在iPad上可以使用UIPopoverController实现这个功能,popoverController继承自NSObject而不是UIVie ...

  9. Axure如何建立共享项目、如何编辑共享项目、如何获取共享项目

    如果小伙伴是一名编程工作者,我们不可以避免的会和Axure这块软件握手,但是但一个项目需要协同操作的时候,就需要用到项目共享的功能.类似我们的svn,想起机房合作的时候,每天早上来到427的我们,打开 ...

  10. Java进阶(一)Java内存解析

    栈.堆.常量池等虽同属Java内存分配时操作的区域,但其适用范围和功用却大不相同.本文将深入Java核心,简单讲解Java内存分配方面的知识. 首先我们先来讲解一下内存中的各个区域. stack(栈) ...