LeetCode #223 - Rectangle Area - Easy
来源:互联网 发布:2016淘宝商品排行榜 编辑:程序博客网 时间:2024/05/19 14:17
Problem
Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the total area is never beyond the maximum possible value of int.
Example
Rectangle Area
Algorithm
整理一下题意:给定二维平面上两个两边与坐标轴平行的矩形,要求返回他们在平面上所占用的面积。假设所有坐标都是整数且在int定义的范围内。
数学题,其实只要把所有情况都考虑到就可以了。所占用的面积等于两个矩形面积之和减去重合面积,于是只要求重合部分的面积即可。可利用数轴来考虑全部情况。
代码如下。
class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int x,y; if(C<=E) x=0; if(A<=E&&E<C&&C<=G) x=C-E; if(E<A&&A<=G&&G<C) x=G-A; if(E<A&&C<=G) x=C-A; if(A<=E&&G<C) x=G-E; if(G<=A) x=0; if(D<=F) y=0; if(B<=F&&F<D&&D<=H) y=D-F; if(F<B&&B<=H&&H<D) y=H-B; if(F<B&&D<=H) y=D-B; if(B<=F&&H<D) y=H-F; if(H<=B) y=0; return abs(A-C)*abs(B-D)+abs(E-G)*abs(F-H)-x*y; }};
0 0
- LeetCode #223 - Rectangle Area - Easy
- [Leetcode 207, Easy] Rectangle Area
- Rectangle Area - LeetCode 223
- leetcode 223: Rectangle Area
- leetcode[223]:Rectangle Area
- Leetcode 223 Rectangle Area
- leetcode 223:Rectangle Area
- leetcode-223 Rectangle Area
- Leetcode #223 Rectangle Area
- leetCode #223 Rectangle Area
- leetcode 223: Rectangle Area
- [Leetcode]#223 Rectangle Area
- [LeetCode 223] Rectangle Area
- [leetcode 223] Rectangle Area
- LeetCode 223 Rectangle Area
- LeetCode 223:Rectangle Area
- 【LEETCODE】223-Rectangle Area
- leetcode 223: Rectangle Area
- [Java] Swing Tab 用法
- 【数据结构】二叉排序树的删除
- poj1741:Tree (树上点分治/treap+启发式合并)
- Spring 初探(二)(Spring JDBC交互简单例子)
- 阿里云价值2万的云服务器邀请码免费拿
- LeetCode #223 - Rectangle Area - Easy
- 使用数位板遇到的常见问题及解决方法
- 交叉编译bluez-5.3及解决No default controller available文件解决
- 学习AngularJs:Directive指令用法(完整版)
- insmod: can't insert 'led.ko': invalid module format详细解释
- Android 进程保活招式大全
- 微信扫码支付-Native原生支付
- JAVA中HashMap和Hashtable区别
- CentOS /RHEL 系统更新安全补丁的方法