【LeetCode】223 - Rectangle Area
2015-08-02 20:08
302 查看
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.
Solution: 两长方形面积之和减去重合部分面积;当A>=G或C<=E或B>=H或D<=F时,两长方形不相交;
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.
Solution: 两长方形面积之和减去重合部分面积;当A>=G或C<=E或B>=H或D<=F时,两长方形不相交;
class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int width,height; if(A>=G||C<=E||B>=H||D<=F){ width=0; height=0; }else{ if(A<=E) width=min(C-E,G-E); else width=min(C-A,G-A); if(B<=F) height=min(D-F,H-F); else height=min(D-B,H-B); } return (C-A)*(D-B)+(G-E)*(H-F)-height*width; } };
相关文章推荐
- angularjs源码阅读——injector
- angularjs源码阅读——invokeLater
- gtk+修改控件文本字体一例
- gtk+修改控件文本字体一例
- LeetCode #65 Valid Number
- gtk+修改控件文本字体一例
- 手机模板修改
- STM32F030 WWDG使用结论
- 欢迎使用CSDN-markdown编辑器
- 【暑假集训】之被ACM金牌大神虐的第三天之树形dp篇
- HDU 2187 悼念512汶川大地震遇难同胞——老人是真饿了【贪心】
- Java内存管理原理及内存区域详解
- java中的异常详解
- 解决Sublime Text 2中文显示乱码的问题
- leetcode 62: Unique Paths
- 小白鼠与毒药解题过程分析
- Java语言程序设计进阶----第四周作业
- 写一个函数,模拟strstr()函数
- 【STL源码剖析】令人困惑的语法
- hdoj5327