LeetCode *** 223. Rectangle Area
2016-04-09 21:08
316 查看
题目:
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.
分析:
先查看是否重合,如果重合,那么找出重合长方形的长宽即可。
代码:
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.
分析:
先查看是否重合,如果重合,那么找出重合长方形的长宽即可。
代码:
class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int aLength=C-A,aWidth=D-B; int bLength=G-E,bWidth=H-F; int area1=aLength*aWidth; int area2=bLength*bWidth; if((G>A&&E>=C)||(C>E&&A>=G)||(D>F&&B>=H)||(H>B&&F>=D)) return area1+area2; else{ int length=aLength+bLength-max(A,C,E,G)+min(A,C,E,G); int width=aWidth+bWidth-max(B,D,F,H)+min(B,D,F,H); return area1+area2-length*width; } } int max(int a,int b,int c,int d){ int res1,res2; res1=a>b?a:b; res2=c>d?c:d; return res1>res2?res1:res2; } int min(int a,int b,int c,int d){ int res1,res2; res1=a<b?a:b; res2=c<d?c:d; return res1<res2?res1:res2; } };
相关文章推荐
- 数据结构顺序表的代码实现
- Java编程:组合、继承和代理的区别
- 1603 - Minimum Sum
- 古老编程环境展--- Windows篇
- 干货--JMS(java消息服务)整合Spring项目案例
- 个人笔记本
- 杭州电子科技大学(HDU)ACM 刷题--------------字符串之2026
- 矩阵下三角元素和
- oracle RAC安装 之 扫不到共享盘
- Hadoop家族学习路线图-张丹老师
- 有个书房+桌椅,会大大增强工作效率
- ztree 2.0 节点搜索
- 2546 饭卡(01背包,挺好的)
- 基于XAMPP(MYSQL+APACHE)快速搭建TESTLINK系统
- iOS开发~CocoaPods使用详细说明
- 合并果子
- “结对编程”——四则运算
- hdu2848 Visible Trees (容斥原理)
- JavaScript数组增加新的项
- [机器学习] Coursera ML笔记 - 逻辑回归(Logistic Regression)