LeetCode 223. Rectangle Area
2016-04-19 10:23
435 查看
Easy level which probably means easy to make mistakes......
Better to draw a graph to make the question clear.....
Under these situations, there is no overlap.
Thus, G <= A || E >= C || G <= B || F >= D.... there is no overlap, the total area is the sum of two cubes area
For the overlap situation, we just need to consider one situation, all the overlaps will be the same anyway....
The easiest one would be one is totally inside the other one.
In this case, the overlap area woud be overlap = abs(min(G, C) - max(A, E)) * abs(max(B, F) - min(D, H));
Thus the total area is The sum of two cubes area - overlap.....
Generate code would be easy then.....
Better to draw a graph to make the question clear.....
Under these situations, there is no overlap.
Thus, G <= A || E >= C || G <= B || F >= D.... there is no overlap, the total area is the sum of two cubes area
For the overlap situation, we just need to consider one situation, all the overlaps will be the same anyway....
The easiest one would be one is totally inside the other one.
In this case, the overlap area woud be overlap = abs(min(G, C) - max(A, E)) * abs(max(B, F) - min(D, H));
Thus the total area is The sum of two cubes area - overlap.....
Generate code would be easy then.....
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { if(E >= C || G <= A || H <= B || D <= F) return (C-A)*(D-B)+(G-E)*(H-F); else { return (C-A)*(D-B)+(G-E)*(H-F) - (min(C, G) - max(A, E)) * (min(H, D) - max(B, F)); } }
相关文章推荐
- 用JDBC编程的运行时错误及其解决大全--转载http://www.th7.cn/Program/java/201409/274583.shtml
- CentOS安装git
- linux centos下安装 R
- 自定义UILabel设置垂直方向的居上,居中,居下
- 八大排序算法
- Android Studio Tips Of the Day
- 移动布局box||-webkit-box
- 大小端模式
- Sql order by 数据排序 优先级问题
- opencv源码解析之——高斯滤波GaussianBlur()
- spark集成hive
- Oracle创建表_01
- mysqldump流程
- Delphi2007新功能 -- 有限的栈对象
- 在IFrame中查找IFRAME中的元素的方式
- ARM中断体系结构
- 2016腾讯实习生面试题(前端、后台、开发、算法)
- 快速启动工具Rulers 3.6
- 在IFrame中查找IFRAME中的元素的方式
- Android软键盘隐藏,遮挡EidtText解决办法