LeetCode(41)-Rectangle Area
2016-04-11 10:08
363 查看
题目:
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.
思路:
题意是要求两个正方形覆盖的面积,他们必然要有相交的部分,两长方形分别求面积相加,减去相交的部分判断有没有相交的部分
-
代码:
public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int area = (D - B) * (C - A) + (H - F) * (G - E); if (A >= G || B >= H || C <= E || D <= F) { return area; } int top = Math.min(D, H); int right = Math.min(C, G); int bottom = Math.max(B, F); int left = Math.max(A, E); return area - (top - bottom) * (right - left); } }
相关文章推荐
- LeetCode(41)-Rectangle Area
- windows 保存rgb数据为bmp文件
- HTTP基本认证(Basic Authentication)的JAVA示例
- SQL优化原则 编辑
- ZOJ 3923 Handshakes
- 浅谈php字符串
- Zookeeper 安装和配置---学习三
- 关于如何提高研发部门工作效率的探讨
- 在shell脚本中调用另一个脚本的三种不同方法(fork, exec, source)
- 判断List、Map、Set是否为空及效率比较
- jQuery基础事件
- Android AIDL学习
- Gradle compile依赖jar,GroupId,ArtifactId查询
- 京东2016笔试题,小东复习
- HTML5常用标签
- 解决几个编译警告我和错误问题
- SecureCRT 7.3.4破解版(含注册机)
- 小米3手机重置
- Glide用在CircleImageView上每次进入第一次显示默认图片
- MVC5中路由新特性