翻译

找到在二维平面中两个相交矩形的总面积。

每一个矩形都定义了其左下角和右上角的坐标。

(矩形例如以下图)

如果,总占地面积永远不会超过int的最大值。

原文

分析

这题前天试过,写了一堆推断。终究还是无果……

贴几个别人的解决方式……

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int64_t xmin1 = min( A, C );
int64_t xmax1 = max( A, C ); int64_t ymin1 = min( B, D );
int64_t ymax1 = max( B, D ); int64_t xmin2 = min( E, G );
int64_t xmax2 = max( E, G ); int64_t ymin2 = min( F, H );
int64_t ymax2 = max( F, H ); int64_t xa = min( xmax1, xmax2 ) - max( xmin1, xmin2 );
int64_t ya = min( ymax1, ymax2 ) - max( ymin1, ymin2 ); int64_t z = 0, ca = max( xa, z ) * max( ya, z ); int64_t a1 = (xmax1 - xmin1) * (ymax1 - ymin1);
int64_t a2 = (xmax2 - xmin2) * (ymax2 - ymin2); return a1 + a2 - ca;
}
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int overlap = (min(C,G)-max(A,E))*(min(D,H)-max(B,F));
if ( min(C,G)<=max(A,E) || min(D,H)<=max(B,F) )
overlap = 0;
return (C-A)*(D-B)+(G-E)*(H-F)-overlap;
}
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int common = (C <= E || A >= G || B >= H || D <= F) ? 0 : (min(C, G) - max(A, E)) * (min(D, H) - max(B, F));
return (C - A) * (D - B) + (G - E) * (H - F) - common;
}

LeetCode 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. 223 Rectangle Area 矩形面积

    在二维平面上计算出两个由直线构成的矩形叠加覆盖后的面积. 假设面积不会超出int的范围. 详见:https://leetcode.com/problems/rectangle-area/descrip ...

  4. [LeetCode] Rectangle Area 矩形面积

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

  5. (easy)LeetCode 223.Rectangle Area

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

  6. Java for 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 : 223. Rectangle Area

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

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

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

随机推荐

  1. RabbitMQ inequivalent arg 'durable' for exchange 'csExchange' in vhost '/': received

    错误:inequivalent arg 'durable' for exchange 'csExchange' in vhost '/': received 使用不同的MQ客户端时,常常会出现以上错误 ...

  2. vim 插件之NERD tree

    NERD tree 这个插件可以用来快速浏览目录结构,打开文件 地址 http://www.vim.org/scripts/script.php?script_id=1658 https://gith ...

  3. POJ TOYS(叉积的妙用+二分答案)

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16042   Accepted: 7688 Description ...

  4. Android5.0之后的页面切换动画

    Android5.0之后给我们开发者剩了好多的事情,为什么这么说呢?还记得刚开始的时候,Android里面的所有的动画都要我们开发者自己来写,现在不需要了,因为5.0之后自带了好多的动画,比如:按钮点 ...

  5. Android-加载大图避免OOM

    高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这些图片都会大于我们程序所需要的大小.比如说系统图片库里展示的图片大都是 ...

  6. PostgreSQL两种事务隔离级

    PostgreSQL两种事务隔离级别: 读已提交:PostgreSQL中缺省隔离级别.当一个事务运行在这个隔离级别时,一个SELECT查询只能看到查询开始之前提交的数据而永远无法看到未提交的数据或者在 ...

  7. 如何在一个 U 盘上安装多个 Linux 发行版

    作者: Ambarish Kumar 译者: LCTT geekpi 本教程介绍如何在一个 U 盘上安装多个 Linux 发行版.这样,你可以在单个 U 盘上享受多个现场版live Linux 发行版 ...

  8. type与isinstance使用区别

    Python中,type与isinstance都可以用来判断变量的类型,但是type具有一定的适用性,用它来判断变量并不总是能够获取到正确的值. Python在定义变量的时候不用指明具体的的类型,解释 ...

  9. flex属性的取值

    首先明确一点是, flex 是 flex-grow.flex-shrink.flex-basis的缩写.故其取值可以考虑以下情况:flex 的默认值是以上三个属性值的组合.假设以上三个属性同样取默认值 ...

  10. TCP学习前的准备——可靠数据传输协议

    由于传输层所依赖的网络层是不可靠的,通过逐渐考虑实际情况不断引入新技术来实现可靠数据传输. 完全可信的信道 有比特差错的信道 新的协议功能: 1.    差错检测:检验和 2.    接收方反馈:序号 ...