LeetCode 223 Rectangle Area(矩形面积)
翻译
找到在二维平面中两个相交矩形的总面积。
每一个矩形都定义了其左下角和右上角的坐标。
(矩形例如以下图)
如果,总占地面积永远不会超过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(矩形面积)的更多相关文章
- [LeetCode] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- [LeetCode]223. Rectangle Area矩形面积
/* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...
- 223 Rectangle Area 矩形面积
在二维平面上计算出两个由直线构成的矩形叠加覆盖后的面积. 假设面积不会超出int的范围. 详见:https://leetcode.com/problems/rectangle-area/descrip ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- (easy)LeetCode 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java for LeetCode 223 Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java [Leetcode 223]Rectangle Area
题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...
- LeetCode : 223. Rectangle Area
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
- [LeetCode] 850. Rectangle Area II 矩形面积之二
We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...
随机推荐
- [Android Studio 权威教程]配置出“NB”的Android Studio
前几篇博客我们已经安装好了As,并且创建了我们的第一个HelloWrod ,这片blog我们继续配置出一个NB的Android Studio 假设你是一个才開始接触到AS或者想从Eclipse转型到A ...
- H5学习_番外篇_PHP数据库操作
1. 文件操作 1.1 打开关闭文件 fopen() resource fopen ( string filename, string mode [, bool use_include_path [, ...
- Tomcat的安装跟配置
安装Tomcat的步骤:1)安装好JDK2)把tomcat-7.0.30软件解压到本地硬盘3)设置环境变量:JAVA_HOME: C:\Program Files\Java\jdk1.7.0_04To ...
- angularjs $http 服务
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- 文件共享服务器nfs搭建过程
网络文件共享服务器192. yum install -y nfs-utils 在exports文件中添加的从机范围 vim /etc/exports /home/nfs/ (rw,sync,fsid= ...
- 28.STL常用算法
#include <algorithm> 算法 常用版本 描述 返回Type std::find() find(_InIt _Fisrt,_InIt _Last, _Ty& _Va ...
- IE11 mobile 的 UA(User-Agent)
如果您在 IE11 mobile 上获得的 UA(User-Agent) 是 Mozilla/5.0 (Mobile; Linux; Android 4.2; Microsoft Build/Virt ...
- PHP接收GET中文参数乱码的原因及解决方案
方案1: $str = iconv("gb2312","utf-8",$str); 方案2: mb_convert_encoding($str, "u ...
- opencv数据结构与基本绘图
#include <opencv2\core\core.hpp>//核心组件 #include <opencv2\opencv.hpp>//GUI,包含媒体输入输出,视频捕捉. ...
- 极速响应Excel数据报表请求的一种方法
摘要 通过缓存和维护Excel Workbook实例,极速响应Excel数据报表请求. 这是一个真实的大数据"云计算"项目中的解决方案,在给定的时间和资源下,只有这种方法是最简单并 ...