LeetCode OJ 223.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.

Assume that the total area is never beyond the maximum possible value of int.
对于这个问题,如果思路正确,解决起来还是比较简单的。
1.如果没有重叠,则直接返回两个矩形的总面积:(C-A)*(D-B) + (G-E)*(H-F)。判断两个矩形是否重叠:if(C<E || G<A || H<B || D<F)。
2.如果有重叠,计算重叠面积的大小。总面积-重叠面积。
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int total = (C-A)*(D-B) + (G-E)*(H-F);
if(B>H || C<E || D<F || A>G) return total;
int l = 0;
if(E>A) l = Math.min((C-E),(G-E));
else l = Math.min((C-A),(G-A));
int h = 0;
if(D>H) h = Math.min((H-B),(H-F));
else h = Math.min((D-B),(D-F));
return (total-(l*h));
}
}
计算重叠部分的长和宽是一个难点,要明确可能重叠的几种情况,然后用简洁的代码把它表示出来。
LeetCode OJ 223.Rectangle Area的更多相关文章
- 【刷题-LeetCode】223. Rectangle Area
Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...
- 【一天一道LeetCode】#223. Rectangle Area
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Find th ...
- 【LeetCode】223. Rectangle Area 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...
- 【LeetCode】223 - Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- [LeetCode] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- Java for LeetCode 223 Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- (easy)LeetCode 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java [Leetcode 223]Rectangle Area
题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...
- LeetCode : 223. Rectangle Area
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
随机推荐
- redis34--string 操作
String类型操作 1.set key value 设置key对应的值为string类型的value 2.mset key1 value1 - keyN valueN 一次设置多个key的值 3. ...
- AngularJS基础总结
w3shools angularjs教程 wiki <AngularJS权威教程> Introduction AngularJS is a JavaScript framewo ...
- C#中ref和out的区别浅析
这篇文章主要介绍了C#中ref和out的区别浅析,当一个方法需要返回多个值的时候,就需要用到ref和out,那么这两个方法区别在哪儿呢,需要的朋友可以参考下 在C#中通过使用方法来获取返回值时,通 ...
- linux deepin 12.12.1和Windows 7双系统修改默认启动项
1.打开终端,输入 sudo gedit /etc/default/grub ,然后输入密码.2.在出来的文本编辑器中找到 GRUB_DEFAULT=0 一行,把0改为2(因为我的Windows 7在 ...
- 5.Hibernate实现全套增删改查和ajax异步分页
1.1 创建如下oracle数据库脚本 drop sequence seq_stu; create sequence SEQ_STU minvalue maxvalue start increment ...
- openwrt源码下载地址(镜像)
与openwrt.org的源码svn路径仅仅多了一个.cn svn://svn.openwrt.org.cn/openwrt/branches/backfiresvn://svn.openwrt.or ...
- luci页面“save&apply”的实现分析
页面上配置的“保存&应用”功能的实现: 最终调用到/etc/config/ucitrack的配置文件. 例如配置无线时,对应ucitrack配置文件中的config network op ...
- ElasticSearch(2)-文档
上一篇 ES(1) 官网原地址:https://www.elastic.co/guide/en/elasticsearch/reference/1.7/_cluster_health.html ES权 ...
- 【转】Storm并行度详解
1.Storm并行度相关的概念 Storm集群有很多节点,按照类型分为nimbus(主节点).supervisor(从节点),在conf/storm.yaml中配置了一个supervisor,有多个槽 ...
- 用Bash脚本将Linux普通用户添加为系统管理员
将Linux普通用户添加为系统管理员在Gnome或KDE这样强大与完善的桌面环境下是非常简单的事情,一般来说在用户设置的对话框里就直接有相应选项.不过,出于简洁与高效的风格,自己目前并未使用这些高端但 ...