95-Rectangle Area
2016-05-27 14:05
232 查看
-223. Rectangle Area My Submissions QuestionEditorial Solution
Total Accepted: 37901 Total Submissions: 126073 Difficulty: Easy
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.
Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.
求两个矩形并集的总面积
思路:求出交集
利用容斥原理
|A⋃B|=|A|+|B|−|A⋂B|
考虑特殊用例:
比如矩形收缩为点
矩形收缩为线
矩形面积越界
整型求和越界
无交集
有交集
Total Accepted: 37901 Total Submissions: 126073 Difficulty: Easy
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.
Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.
求两个矩形并集的总面积
思路:求出交集
利用容斥原理
|A⋃B|=|A|+|B|−|A⋂B|
考虑特殊用例:
比如矩形收缩为点
矩形收缩为线
矩形面积越界
整型求和越界
无交集
有交集
class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { long long resAx,resBy,resCx,resDy; long long area1,area2,intersect=-1; resAx= max(A,E); resCx= min(C,G); resBy=max(B,F); resDy=min(D,H);//求出交集矩形对角点 if(resCx-resAx<=0||resDy-resBy<=0)intersect=0; //交集为空 else intersect = abs(resCx-resAx)*abs(resDy-resBy); if((A==C||B==D)||(E==G)||F==H)intersect=0;//修补收缩为线的情况 area1=(C-A)*(D-B),area2 = (G-E)*(H-F); return area1+area2-intersect; } };
相关文章推荐
- android数据库操作优化(二)
- iOS-如何通过UIEdgeInsetsMake来制作可伸缩的Button
- Spark Streaming揭秘 Day15 No Receivers方式思考
- 申办上海市居住证所需的材料
- Netfilter
- svn 合并分支代码
- NodeJs 搭建简单的聊天室
- JSP标签 <meta> 的作用
- python 发送邮件实例
- Xcode is mossing from working copy
- SDF与MDF的区别
- android数据库操作优化(一)
- LA2995
- 递归函数练习
- Volley 源码解析
- 虚拟机 centos7 安装jdk7
- 数据库Mysql性能优化详解
- java应该知道什么
- layer使用总结一配置
- 了解thinkphp(五)