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. 初识SilkTest

    简介 编辑 SilkTest 是业界领先的.用于对企业级应用进行功能测试的产品,可用于测试Web.Java或是传统的C/S结构.SilkTest 提供了许多功能,使用户能够高效率地进行软件自动化测试. ...

  2. ubuntu 16.04下搭建web服务器(MySQL+PHP+Apache) 教程

    1.开始说明 下面很多可能参照网上其中以为前辈的,但有所改进吧.这些设置可能会有所不同,你需要根据不同情况进行修改. 安装apache2 2.切换管理员身份 在ubuntu中需要用root身份进行操作 ...

  3. 身份证js检测

    var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古&quo ...

  4. HDU猜数字

    G - 猜数字 Time Limit:10000MS       Memory Limit:32768KB       64bit IO Format:%I64d & %I64u Descri ...

  5. event 实现观察者模式

    看了一些其他人写的,一下就晕了,还是自己写一个给自己看吧. 用event语法糖实现的观察者,与普通的实现,最大的区别在于,Subject的操作中不会直接触发Observer的Update,而是通过ev ...

  6. openstack私有云布署实践【3 keepalive配置(VRRP虚IP)】

    这里只用到keepalived 或者pacemaker两种方法,可二选一来配置,官方提供的是pacemaker,但我们实际测试环境和生产环境上使的是keepalive     首先在4台control ...

  7. java ee开发报错

    七月 26, 2015 9:57:52 下午 org.apache.coyote.AbstractProtocol destroy信息: Destroying ProtocolHandler [&qu ...

  8. 1、Spring概述

    Java EE优缺点 我们都知道在2003年Spring兴起之前,企业普遍使用J2EE技术来开发企业级应用,为什么用J2EE呢?主要原因有以下几个: 1.Java本身的跨平台能力,可移植性强2.J2E ...

  9. AVL树 高度平衡的二叉查找树

    1.What is AVL tree? AVL tree 是一种特殊的二叉查找树,,首先我们要在树中引入平衡因子balance,表示结点右子树的高度减去左子树的高度差(右-左),对于一棵AVL树要么它 ...

  10. Linux中kettle启动spoon.sh遇到的问题

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAccAAAAYCAIAAAAAgaGrAAAE1klEQVR4nO1b2ZWrMAylLgpyPa7Gzb