223. Rectangle Area
2015-11-25 04:17
495 查看
题目:
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.
![](https://leetcode.com/static/images/problemset/rectangle_area.png)
Assume that the total area is never beyond the maximum possible value of int.
链接: http://leetcode.com/problems/rectangle-area/
题解:
数学题,需要判断矩阵是否相交,相交的话减去重复面积(顶点相交除外)。
Time Complexity - O(1), Space Complexity - O(1)。
二刷:
方法跟一刷一样
Java:
Time Complexity - O(1), Space Complexity - O(1)。
三刷:
Java:
一开始先计算出两个矩形的面积和 totalArea
判断两个矩形是否相交,假如不相交,或者仅有顶点相交,那么我们直接返回totalArea。 这里两个矩形 x 的范围是 (A, C), (E, F), y的范围是(B, D), (E, F)
计算overlap的面积,边的计算公式是 ( 最小的上方或者右方点 - 最大的下方或者左方点), 相乘就是overlap的面积
相减得到结果
Reference:
https://leetcode.com/discuss/39188/an-easy-to-understand-solution-in-java https://leetcode.com/discuss/39398/my-java-solution-sum-of-areas-overlapped-area https://leetcode.com/discuss/43549/just-another-short-way https://leetcode.com/discuss/43173/if-you-want-to-laugh-look-at-my-solution https://leetcode.com/discuss/54138/python-concise-solution https://leetcode.com/discuss/51354/an-explanation-in-plain-language http://www.cnblogs.com/0001/archive/2010/05/04/1726905.html http://www.geeksforgeeks.org/find-two-rectangles-overlap/ https://www.cs.princeton.edu/~rs/AlgsDS07/17GeometricSearch.pdf
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.
![](https://leetcode.com/static/images/problemset/rectangle_area.png)
Assume that the total area is never beyond the maximum possible value of int.
链接: http://leetcode.com/problems/rectangle-area/
题解:
数学题,需要判断矩阵是否相交,相交的话减去重复面积(顶点相交除外)。
Time Complexity - O(1), Space Complexity - O(1)。
public class Solution { 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 duplicate = (Math.min(C, G) - Math.max(A, E)) * (Math.min(D, H) - Math.max(B, F)); return area - duplicate; } }
二刷:
方法跟一刷一样
Java:
Time Complexity - O(1), Space Complexity - O(1)。
public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int totalArea = (C - A) * (D - B) + (G - E) * (H - F); if (A >= G || B >= H || C <= E || D <= F) { return totalArea; } int sameArea = (Math.min(C, G) - Math.max(A, E)) * (Math.min(D, H) - Math.max(B, F)); return totalArea - sameArea; } }
三刷:
Java:
一开始先计算出两个矩形的面积和 totalArea
判断两个矩形是否相交,假如不相交,或者仅有顶点相交,那么我们直接返回totalArea。 这里两个矩形 x 的范围是 (A, C), (E, F), y的范围是(B, D), (E, F)
计算overlap的面积,边的计算公式是 ( 最小的上方或者右方点 - 最大的下方或者左方点), 相乘就是overlap的面积
相减得到结果
public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int totalArea = (C - A) * (D - B) + (G - E) * (H - F); if (A >= G || C <= E || B >= H || D <= F) { return totalArea; } int overlap = (Math.min(C, G) - Math.max(A, E)) * (Math.min(D, H) - Math.max(B, F)); return totalArea - overlap; } }
Reference:
https://leetcode.com/discuss/39188/an-easy-to-understand-solution-in-java https://leetcode.com/discuss/39398/my-java-solution-sum-of-areas-overlapped-area https://leetcode.com/discuss/43549/just-another-short-way https://leetcode.com/discuss/43173/if-you-want-to-laugh-look-at-my-solution https://leetcode.com/discuss/54138/python-concise-solution https://leetcode.com/discuss/51354/an-explanation-in-plain-language http://www.cnblogs.com/0001/archive/2010/05/04/1726905.html http://www.geeksforgeeks.org/find-two-rectangles-overlap/ https://www.cs.princeton.edu/~rs/AlgsDS07/17GeometricSearch.pdf
相关文章推荐
- mysql改为mysqli几项注意
- HDU 4005 The war(tarjan bcc + dp)
- JUnit简单使用教程
- HDU 4203 Doubloon Game(博弈打表)
- 欢迎使用CSDN-markdown编辑器
- 随机排序
- 【学习】二叉树基本操作
- Netty Sharable Handler 使用是私有变量
- HDU 3590 PP and QQ(树上删边游戏 + anti-sg)
- [岁月随想]感恩节前夕
- HDU 3094 A tree game(树上删边游戏)
- leetcode之Delete Node in a Linked List
- [Redux] Store Methods: getState(), dispatch(), and subscribe()
- No 11.11 in my dictionary
- CF Round#333 Div2 C. The Two Routes
- Android-存储方式(持久化数据的方式)
- [Redux] Introduction
- FFmpeg 学习资料
- node.js开发小项目的全过程 1 快速创建项目
- 双链表(C实现)