[LeetCode] Rectangle Area
2015-06-08 12:30
295 查看
Rectangle Area
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.
Credits:
Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.
数学题,求矩形覆盖面积。因为只有两个矩形,所以直接算结果 = 两个矩形的面积 - 相交的面积。估计下一道题就会是求多个矩形的覆盖面积了吧。
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.
Credits:
Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.
数学题,求矩形覆盖面积。因为只有两个矩形,所以直接算结果 = 两个矩形的面积 - 相交的面积。估计下一道题就会是求多个矩形的覆盖面积了吧。
class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int res = (D - B) * (C - A) + (H - F) * (G - E); int A1 = max(A, E), B1 = max(B, F), C1 = min(C, G), D1 = min(D, H); if (D1 <= B1 || C1 <= A1) return res; return res - (D1 - B1) * (C1 - A1); } };
相关文章推荐
- python网络编程
- 学习linux驱动经典书籍
- Exchange 2013 中客户端和邮件流的网络端口
- [工具使用] SOAP UI 测试
- android中图片压缩以及图片旋转的方法
- nodejs - 如何完全更新
- curses.h学习笔记
- kaptcha验证码使用
- 杭电ACM2022java做法
- 怎样配置EBS Mobile 客户端
- vim行首加入某字符
- Vim删除不包含指定字符串的行
- 根据字段去重查询所有数据
- 统计字符串中每个字母出现的次数。
- HangOver
- webview在红米3上面打开显示错误A WebView method was called on thread 'Thread-9191'. All WebView methods must be
- phpStorm8注册码
- Android学习笔记(1)
- ActiveAndroid 管理数据库以及ActiveAndroid 如何管理boolean类型
- python下MySQLdb用法实例分析