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 res=(C-A)*(D-B)+(G-E)*(H-F);
        int A1=Math.max(A,E);
        int B1=Math.max(B,F);
        int C1=Math.min(C,G);
        int D1=Math.min(D,H);
        if(A1>=C1 || B1>=D1) return res;
        return res-(C1-A1)*(D1-B1);
    }

}

 运行结果:

 

(easy)LeetCode 223.Rectangle Area的更多相关文章

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

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

  2. Java for LeetCode 223 Rectangle Area

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

  3. Java [Leetcode 223]Rectangle Area

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

  4. LeetCode : 223. Rectangle Area

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

  5. LeetCode 223 Rectangle Area(矩形面积)

    翻译 找到在二维平面中两个相交矩形的总面积. 每一个矩形都定义了其左下角和右上角的坐标. (矩形例如以下图) 如果,总占地面积永远不会超过int的最大值. 原文 分析 这题前天试过,写了一堆推断.终究 ...

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

    /* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...

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

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

  8. [LeetCode] 850. Rectangle Area II 矩形面积之二

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...

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

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

随机推荐

  1. 转载:使用sklearn做单机特征工程

    目录 1 特征工程是什么?2 数据预处理 2.1 无量纲化 2.1.1 标准化 2.1.2 区间缩放法 2.1.3 标准化与归一化的区别 2.2 对定量特征二值化 2.3 对定性特征哑编码 2.4 缺 ...

  2. SQL Server 2005 分区表创建实例

    --创建一个分区函数(默认为左边界)CREATE PARTITION FUNCTION PARTFUNC1(INT)AS RANGEFOR VALUES(1000,2000,3000,4000,500 ...

  3. JavaScript判断字符串能否转化为数字

    判断一个字符串能否转化为数字 我们常常使用parseInt函数. 不过该函数的适用范围是很小的. 一般说来对于 如下类似 var myStr = "123hello"; 使用par ...

  4. ios9 URL Schemes列为白名单,才可正常检查其他应用是否安装

    http://my.oschina.net/u/1244672/blog/512210 如果你在模拟器上运行可以能还会有以下报错: -canOpenURL: failed for URL: “weix ...

  5. 使用Lucene.Net实现全文检索

    使用Lucene.Net实现全文检索 目录 一 Lucene.Net概述 二 分词 三 索引 四 搜索 五 实践中的问题 一 Lucene.Net概述 Lucene.Net是一个C#开发的开源全文索引 ...

  6. 重绘Winform窗体

    本文转载自:http://www.cnblogs.com/encoding/p/5603080.html 按照惯例,先来几张样例图(注:为了展示窗口阴影效果,截图范围向外扩展了些,各位凭想象吧). 还 ...

  7. Python 字典的创建赋值和动态扩展

    >>> cleese={} >>> palin=dict() >>> type(cleese) <class 'dict'> > ...

  8. 【转】CSS浏览器兼容性与解析问题终极归纳

    1.怪异模式问题:漏写DTD声明,Firefox仍然会按照标准模式来解析网页,但在IE中会触发怪异模式.为避免怪异模式给我们带来不必要的麻烦,最好养成书写DTD声明的好习惯. 2.IE6双边距问题:在 ...

  9. 真假云主机,VPS资料集合

    资料来源: http://www.west263.com/services/CloudHost/pk.asp?ads=baidu912 用"云里雾里"形容中小企业用户对云计算的理解 ...

  10. Web上传文件

      客户端      相对于FTP文件上传,Web文件上传速度慢一些,但使用方便,不需要客户端,而且权限比FTP容易控制. Web文件上传采用POST方式,上传文件需要设置FORM的entype属性为 ...