在二维平面上计算出两个由直线构成的矩形叠加覆盖后的面积。

假设面积不会超出int的范围。

详见:https://leetcode.com/problems/rectangle-area/description/

Java实现:

class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int sum = (C - A) * (D - B) + (H - F) * (G - E);
if (E >= C || F >= D || B >= H || A >= G){
return sum;
}
return sum - ((Math.min(G, C) - Math.max(A, E)) * (Math.min(D, H) - Math.max(B, F)));
}
}

C++实现:

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

  参考:https://www.cnblogs.com/grandyang/p/4563153.html

223 Rectangle Area 矩形面积的更多相关文章

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

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

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

    /* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...

  3. [LeetCode] Rectangle Area 矩形面积

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

  4. (easy)LeetCode 223.Rectangle Area

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

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

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

  6. [HDU 4419] Colourful Rectangle (扫描线 矩形面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...

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

    翻译 找到在二维平面中两个相交矩形的总面积. 每一个矩形都定义了其左下角和右上角的坐标. (矩形例如以下图) 如果,总占地面积永远不会超过int的最大值. 原文 分析 这题前天试过,写了一堆推断.终究 ...

  8. 223. Rectangle Area

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

  9. LeetCode OJ 223.Rectangle Area

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

随机推荐

  1. easyui英文提示变中文

    近期玩JQuery easyUI,系统默认的日期和文本输入框提示英文.作为一个地道的中国人,是不是提示成中文.日期也显示成中文,是不是更人性化呢,下面为操作方法哦. 更改前效果 1 输入框提示为英文 ...

  2. 【Facebook的UI开发框架React入门之八】Image的使用简单介绍(iOS平台)-goodmao

    --------------------------------------------------------------------------------------------------- ...

  3. awk基本使用方法简单介绍

    之前说过sed, 今天来说awk, 它也是一个文本处理器. 是linux下的一个命令, 比sed更强大. 搞linux开发, 尤其是后台开发, 这个命令差点儿必需要用到. awk这三个字母分别代表其三 ...

  4. [CSAPP]Bufbomb实验报告

    Bufbomb实验报告 实验分析: level 0-3从test開始制运行,通过函数getbuf向外界读取一串内容(buf). Level 4 是通过參数-n,程序运行testn函数,调用getbuf ...

  5. Linux Chromium安装Adobe Flash Player

    首先,下载: install_flash_player_11_linux.i386.tar.gz 解压文件: tar -xvf install_flash_player_11_linux.i386.t ...

  6. 契约式设计 契约式编程 Design by contract

    Design by contract - Wikipedia https://en.wikipedia.org/wiki/Design_by_contract What is the use of & ...

  7. 用css解决Unigui在IE系列浏览器中字体变小的问题(设置UniServeModule的customcss属性)

    Unigui运行在chrome浏览器下可以有最佳的效果,但用ie打开用unigui做的项目会发现字体明显小一截,可以用自定义css来解决这个问题. 可以在UniServeModule的customcs ...

  8. RFC函数设置外部断点

  9. 在调试状态查看DateTable里的数据信息

  10. linux内存管理之uboot第一步

    在进入讲解linux内存管理的kernel阶段以前,了解一下uboot阶段是如何准备好内存物理设备的,这是非常有意义的.通常进入到linux内核阶段之后,对内存芯片的物理特性寄存器访问是比较少的,强调 ...