开始刷leetcode day79:Rectangle Area
2015-07-28 13:00
211 查看
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.
Java:
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int sq1 = (C-A)*(D-B);
int sq2 = (G-E)*(H-F);
long left = (long)Math.min(C,G) - (long)Math.max(A,E);
if(left <0) left = 0;
long right = (long)Math.min(D,H) - (long)Math.max(B,F);
if(right <0) right = 0;
return (int)(sq1+sq2 - left*right);
}
}
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.
Java:
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int sq1 = (C-A)*(D-B);
int sq2 = (G-E)*(H-F);
long left = (long)Math.min(C,G) - (long)Math.max(A,E);
if(left <0) left = 0;
long right = (long)Math.min(D,H) - (long)Math.max(B,F);
if(right <0) right = 0;
return (int)(sq1+sq2 - left*right);
}
}
相关文章推荐
- Androidstudio 安装codota插件
- [转]RC4加密已不再安全,破解效率极高
- 源码编译MySQL5.6.24
- [CareerCup] 3.7 Adopt Animal 领养动物
- Android 界面滑动卡顿分析与解决方案(入门)
- HDU-3631 Shortest Path (floyd)
- druid 连接池配置
- 如何转换鄚咖驼世界地图投影坐标
- 使用自定义模板为Myeclipse添加新建Filter的功能
- java虚拟机优化--gc日志
- (剑指Offer)面试题41:和为s的连续正数序列
- 为网页添加蒙板效果和弹出层js
- MFC 数据类型转化实例
- 萝卜叶万能助手SEO网络营销简介
- Algorithms—238.Product of Array Except Self
- 欧几里得算法求解最大公约数和最小公倍数
- oracle 临时表空间的增删改查
- 建造者模式-Builder Pattern
- Spring中ThreadLocal的认识
- HDU 2018 母牛的故事