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, ...
随机推荐
- ,典型递归问题-F(1025)mod 5 的值
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- MyBatis+mysql查询和添加数据
项目结构: Menu package com.mstf.dao; import java.util.Scanner; import org.apache.ibatis.session.SqlSessi ...
- PostgreSQL环境中查看SQL执行计划示例
explain analyze ,format,buffers, format :TEXT, XML, JSON, or YAML. EXPLAIN (ANALYZE,buffers,format ...
- <Sicily>Polynomial
一.题目描述 Given a polynomial and the value of the variable x, you task is to calculate the value of the ...
- 联想 M415 I3-6100 CPU安装系统方法
问题: 直接用PE GHOST系统后,USB无法使用,导致鼠标.U盘也无法使用 即 无法安装驱动.软件等 方法: 1.按网上方式,安装集成USB3.0的PE系统 2. 直接用PS2鼠标安装
- 几个提高效率的PHOTOSHOP秘密快捷键
1.拖动选择 使用矩形选框工具,在画布上拖动(不要松开鼠标),这时按住空格键,然后移动鼠标,你会发现选区也跟着移动了. 2.左右流量文档 按住Cmd(Ctrl)键,上下滚动鼠标,你会发现文档的滚动条在 ...
- url链接打开本地应用(测试通过)
基于windows!! 类比mailto://XXXX 主要参考: https://www.cnblogs.com/snow365/p/6428212.html 应用 1.在网页上本地办公 网页应用越 ...
- NodeJS代码调试
1.在Chrome打开chrome://flags/#enable-devtools-experiments 2.激活Developer Tools experiments 3.重启Chrome 4. ...
- python_webApp
提高开发效率:当更改代码后,不重启服务器就能使用新效果 参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df ...
- nios sgdma(Scatter-Gather dma)示例
在 Quartus7.2之后的版本中,除了原有的基于avalon-mm总线的DMA之外,还增加了Scatter-Gather DMA这种基于avalon-ST流总线的DMA IP核,它更适合与大量数据 ...