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

假设面积不会超出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. vue自定义轮播图组件 swiper

    1.banner 组件 components/Banner.vue <!-- 轮播图 组件 --> <template> <div class="swiper- ...

  2. 鸟哥的Linux私房菜-----13、账号管理

  3. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

  4. [Android]自己定义带删除输入框

    在项目开发中,带删除button输入框也是人们经常常使用到的,该文章便介绍一下怎样创建一个带删除输入框.当中,须要解决的问题例如以下: a)创建自己定义editText类 b)在自己定义editTex ...

  5. Android碎纸机效果

    1.总体思想 活用padding和margin 2.实现过程 public class PopupShredderView extends FrameLayout{ public PopupShred ...

  6. 构造json参数时key的引号和js string转json的三种方式

    {name:"dd",age:"16"} {"name":"dd","age":"16&q ...

  7. Package vim is not available, but is referred to by another package及我的vim配置

    新安装的ubuntu,先安装vim,但是安装出现 Reading package lists... Done Building dependency tree Reading state inform ...

  8. POJ - 1422 Air Raid(DAG的最小路径覆盖数)

    1.一个有向无环图(DAG),M个点,K条有向边,求DAG的最小路径覆盖数 2.DAG的最小路径覆盖数=DAG图中的节点数-相应二分图中的最大匹配数 3. /* 顶点编号从0开始的 邻接矩阵(匈牙利算 ...

  9. ios app 上架AppStore

    一.证书的导出      1.1 前期工作        首先你需要有一个苹果的开发者帐号,一个Mac系统.        如果没有帐号可以在打开http://developer.apple.com/ ...

  10. 【Jsoi2010】连通数

    [题目链接] 点击打开链接 [算法] 直接暴力dfs一遍,即可 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN ...