LeetCode 223 Rectangle Area
2015-12-11 09:01
323 查看
题目描述
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.
Credits:
Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.
分析
题目要求的是两个矩形总的面积,不是相交的面积……不考虑两个矩形相交,分别求出每个矩形的面积,相加
如果两个矩形不相交,直接返回结果
如果两个矩形相交,减去相交部分面积
代码
[code] public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int area = (C - A) * (D - B) + (G - E) * (H - F); if (A >= G || B >= H || C <= E || D <= F) { return area; } int top = Math.min(D, H); int bottom = Math.max(B, F); int left = Math.max(A, E); int right = Math.min(C, G); return area - (top - bottom) * (right - left); }
相关文章推荐
- iOS开发-进阶:企业应用发布教程
- 网络运营必须掌握的运营法则太原郭文军讲
- 第十四周项目1 折半查找
- 9 To view the results of a manual SQL Tuning Advisor task, which steps should the DBA take? A. From
- 第十二周项目五:迷宫问题之深度优先遍历解法
- NetHack 3.6.0 发布
- 发散思维考虑编码流程
- iOS开发-进阶:发布 在企业内部分发 iOS 应用程序
- Access to the path '....' is denied.解决方法
- in与exist , not in与not exist 的区别 【轉】
- 重载
- 【代码笔记】侧滑效果
- 第四周 项目六 多项式求和
- 【代码笔记】侧滑效果
- 如何看代码的架构
- A-8 8 Which of these appropriately describes the results of a manual SQL Tuning Advisor task? A. A l
- iOS开发-UI控件:修改textField的placeholder的字体颜色、大小
- 谁偷走了我们的时间?
- 第十五周项目二----用哈希法组织关键字之链地址法
- CART回归树&模型树 生成 剪枝 in Python