LeetCode OJ——Submission Details
2015-11-22 21:54
579 查看
题目:
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.
代码:
结果:
思路:
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.
代码:
class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { //计算第一个矩形的面积 int s1 = (C - A) * (D - B); int s2 = (G - E) * (H - F); int point_low[2] = { 0 }; int point_high[2] = { 0 }; //先判断x轴的坐标关系A-C与E-G if (G <= A) //第二个矩形在第一个矩形的左边,两者不相交 { return s1 + s2; } if (G >A && G <= C) //第二个矩形与第一个矩形可能有重叠的部分 { if (H <= B || F >= D) //第二个矩形与第一个矩形不重叠 { return s1 + s2; } else{ point_high[0] = (G < C) ? G : C; point_high[1] = (H < D) ? H : D; point_low[0] = (E > A) ? E : A; point_low[1] = (F > B) ? F : B; return (s1 + s2 - (point_high[0] - point_low[0]) * (point_high[1] - point_low[1])); } } if (G > C) //第二个矩形与第一个矩形可能有重叠的部分 { if (E >= C) //第二个矩形与第一个矩形不重叠 { return s1 + s2; } else{ //第二个矩形与第一个矩形可能有重叠的部分 if (F >= D || H < B) //第二个矩形与第一个矩形不重叠 { return s1 + s2; } else{ point_high[0] = (G < C) ? G : C; point_high[1] = (H < D) ? H : D; point_low[0] = (E > A) ? E : A; point_low[1] = (F > B) ? F : B; } return (s1 + s2 - (point_high[0] - point_low[0]) * (point_high[1] - point_low[1])); } } } };
结果:
思路:
相关文章推荐
- BZOJ4320: ShangHai2006 Homework
- [AlwaysOn Availability Groups]AlwaysOn等待类型
- Domain Name System (DNS)
- NYOJ 150 Train Problem I
- 快速激活JetBrains系列产品 可以激活WebStorm11
- [AlwaysOn Availability Groups]AG扩展事件
- SSH留言板 com.sun.jersey.api.container.ContainerException 错误
- Jade之Plain Text
- LeetCode 子 Climbing Stairs
- jboss服务启动时报Error creating bean with name'addressService': Injection of autowired dependencies faile
- 【LeetCode OJ 011】Container With Most Water
- cat ,tac ,more,less ,head,tail 和文本相关的命令用法
- 2015Beijing区域赛(Today Is a Rainy Day-暴搜)
- CodeForces 579A Raising Bacteria
- AIX 下lsnrctl start 错误
- 服务器配置raid10和两个raid1的区别
- hdu1082 Matrix Chain Multiplication
- HDU 5245 joyful( 2015 Shanghai Metropolitan J )
- 理解Aidl中Stub和Stub.Proxy
- CLSRSC-184 CLSRSC-258: Failed to configure and start ASM