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的更多相关文章

  1. 【刷题-LeetCode】223. Rectangle Area

    Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...

  2. 【一天一道LeetCode】#223. Rectangle Area

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Find th ...

  3. 【LeetCode】223. Rectangle Area 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...

  4. 【LeetCode】223 - Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  5. [LeetCode] 223. Rectangle Area 矩形面积

    Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...

  6. Java for LeetCode 223 Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  7. (easy)LeetCode 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  8. Java [Leetcode 223]Rectangle Area

    题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...

  9. LeetCode : 223. Rectangle Area

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

随机推荐

  1. libev中timer时间事件监控器

    1.数据结构 #define ev_at(w) ((WT)(w))->at#define ev_active(w) ((W)(w))->active typedef ev_watcher_ ...

  2. DMI ( Dynamic Method Invocation )

    功能: 点击 hello , 调用 execute 函数 点击 update , 调用 update 函数 1.项目结构 2.web.xml <?xml version="1.0&qu ...

  3. shell中$0,$?,$!

    变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代码(返回值) $- 使用Set命令设定的Flag一览 ...

  4. linux下安装php的mcrypt拓展

    安装步骤: 1,#wget  http://museum.php.net/php5/php-5.3.3.tar.gz 2,解压:#tar -zxvf  php-5.3.3.tar.gz 3,#cd   ...

  5. SVM与LR的比较

    两种方法都是常见的分类算法,从目标函数来看,区别在于逻辑回归采用的是logistical loss,svm采用的是hinge loss.这两个损失函数的目的都是增加对分类影响较大的数据点的权重,减少与 ...

  6. Spring 后置处理器 PropertyPlaceholderConfigurer 类(引用外部文件)

    一.PropertyPlaceholderConfigurer类的作用 PropertyPlaceholderConfigurer 是 BeanFactory 后置处理器的实现,也是 BeanFact ...

  7. 微信小程序Tabbar文字在真机不显示

    按照官方文档在json中定义好了Tabbar后,在模拟器上显示没问题,而在真机上不显示Tabar文字. 让我很苦笑不得的原因是: 在app.json定义Tabbar文字选中态和非选中态颜色时我用了英文 ...

  8. gRPC编码初探(java)

    背景:gRPC是一个高性能.通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众 ...

  9. Asp.NET路由管道处理过程 【重要】

    当ASP.NET处理请求时,路由管道主要由以下几个步骤组成: 1.UrlRoutingModule尝试使用RouteTable中注册的理由匹配当前请求: 2.如果RouteTable中有一个路由成功匹 ...

  10. luci页面“save&apply”的实现分析

    页面上配置的“保存&应用”功能的实现: 最终调用到/etc/config/ucitrack的配置文件. 例如配置无线时,对应ucitrack配置文件中的config network    op ...