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 sq = (C-A)*(D-B)+(G-E)*(H-F);
long len = (long)Math.min(C,G)-(long)Math.max(A,E);
long we = (long)Math.min(D,H)-(long)Math.max(B,F);
if(len<=0||we<=0){
return sq;
}
return (int)(sq-len*we);
}
}

Rectangle Area——LeetCode的更多相关文章

  1. Rectangle Area || LeetCode

    把交叉点的坐标求出来即可. #define max(a,b) ( (a)>(b)?(a):(b) ) #define min(a,b) ( (a)<(b)?(a):(b) ) int co ...

  2. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

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

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

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

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

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

    223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...

  6. [leetcode] Rectangle Area

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

  7. 【leetcode】Contains Duplicate & Rectangle Area(easy)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

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

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

  9. leetcode之Rectangle Area

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

随机推荐

  1. NC V6 nchome文件目录及其作用介绍

    NC V6发布一段时间了,各个NC6.0 nchome文件夹下各个子文件夹内容和作用 ant:存放Apache Ant,用来执行EJB的构建. bin: 存放nc部署和系统监控等命令.configsy ...

  2. CentOS 5.4下的Memcache安装步骤(Linux+Nginx+PHP+Memcached)

    原文链接:http://www.jb51.net/article/29668.htm

  3. C# Activator.CreateInstance()

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  4. UITableViewCell 左滑删除

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...

  5. VisualStudio2013内置SQLServer入门(二)--增删改查

    前一篇 http://www.cnblogs.com/qixi233/p/4766451.html 这篇是sqlserver的操作 界面比较走心哈哈哈,将就着看,主要就是下面增删改查四个btn 对于s ...

  6. Java反射学习(java reflect)(二)

    ok之前说了Java的反射和反射分析类,那这些东西有神马作用呢,下面就来说应用: 三.运行时使用反射分析对象 简单写一个Employee类,然后利用JAVA反射去取name域,getDeclareFi ...

  7. Html禁止粘贴 复制 剪切

    oncopy="return false;" onpaste="return false;" oncut="return false;"

  8. js submit的問題

    form 里面有input name="submit"的时候 $('#seachform').submit();不起作用

  9. 关于考虑浏览器兼容性时间的工具demo

    //支持跨浏览器的添加事件. var btn = document.getElementById("btn"); function showMes() { alert(" ...

  10. Access自动编号的初始值设置及重置编号 转

    方法如下: ALTER TABLE tableName ALTER COLUMN Id COUNTER (100, 5) 其中:tableName为要修改的表名,Id为自动编号列,100为初始值,5为 ...