Rectangle Area
2015-06-16 10:22
260 查看
Rectangle Area
问题:
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.
View Code
学习之处:
是看了别人的代码才想到了怎么做,因为之前自己的代码太挫了,必须通过各种if else才能得到两个矩形的关系,别人的方法是通过左边界,右边界,上边界,下边界确定了矩形的面试,值得学习。
问题:
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.
public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int oneArea = (C-A)*(D-B); int twoArea = (G-E)*(H-F); int left = Math.max(A, E); int right = Math.min(C, G); int down = Math.max(B, F); int top = Math.min(D, H); int intersect = 0; if(left < right && down < top) intersect = (right-left)*(top-down); return oneArea + twoArea - intersect; } }
View Code
学习之处:
是看了别人的代码才想到了怎么做,因为之前自己的代码太挫了,必须通过各种if else才能得到两个矩形的关系,别人的方法是通过左边界,右边界,上边界,下边界确定了矩形的面试,值得学习。
相关文章推荐
- scrollview嵌套listview
- 聊天室服务器程序
- android 开发环境搭建 eclipse支持,解决dl-ssl.google.com资源下载不动问题
- 《C++语言基础》实践参考——max带来的冲突
- Java去除掉HTML里面所有标签的两种方法——开源jar包和自己写正则表达式
- LeetCode之“动态规划”:Edit Distance
- ASP.NET创建动态缩略图的方法
- XAMPP Mysql 忘记root密码解决方案
- log4net的典型配置
- Android内存优化之封装九宫格
- Moravec算子
- 《征服C指针》——读书笔记(4)
- ZH奶酪:纯CSS自定义Html中Checkbox复选框样式
- 【SSO单点系列】(7):CAS4.0 SERVER通过数据库方式认证用户
- MVC 表单提交【转】
- LaunchImage设置了为什么不显示
- 【Android UI设计与开发】第09期:底部菜单栏(四)Fragment+PopupWindow仿QQ空间最新版底部菜单栏
- 进化聚类算法的相关算法
- Vmware vSphere 5.0系列教程之一 Vmware vSphere 5.0简介
- 【j2ee spring】34、巴巴运动网的产品类别管理