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
随机推荐
- DNS服务架设 redhat linux
安装dns服务和管理工具 yum install bind bind-chroot bind-utils -y 服务名为named 在没有启动服务之前服务的主配置文件在/etc/named.con ...
- snmp协议接口
所有网络设备上都会支持smap,获取服务器的基本信息,这样就不用在客户端上装应用就可以检测到基本的信息,是基于socket开发 内存调用这些命令来提取服务器的信息 snmpgetlocalhost - ...
- 二十六、oracle pl/sql 分页
一.无返回值的存储过程 古人云:欲速则不达,为了让大家伙比较容易接受分页过程编写,我还是从简单到复杂,循序渐进的给大家讲解.首先是掌握最简单的存储过程,无返回值的存储过程. 案例:现有一张表book, ...
- ubuntu 安装python mysql模块
Installation Starting with a vanilla Lucid install , install pip and upgrade to the latest version: ...
- 强制改变IE中的文本模式
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
- 触发器实现对插入数据的字段更改 Oracle+SQL Server
最近有个使用触发器实现对插入数据的某个列做更改的需求,因此整理了Oracle和SQL Server对于此类需求的触发器写法,本文仅提到了Insert触发器. 首先我们创建一张表: --创建Test表 ...
- 淘淘商城_day03_课堂笔记
今日大纲 实现商品的编辑 实现商品的规格参数功能 搭建前台系统 实现首页商品类目的显示 商品的编辑 数据的编辑核心是:数据回显. 编辑按钮事件 判断选中的行数 弹出window 加载编辑页面,在页面加 ...
- The most orzed and orzing man
The most orzed and orzing man 题目链接:http://acm.xidian.edu.cn/problem.php?id=1184 Sprague-Grundy定理:htt ...
- PAT 团体程序设计天梯赛-练习集 L1-020. 帅到没朋友
原题 https://www.patest.cn/contests/gplt/L1-020 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. ...
- spring security 1
首先我们为Spring Security专门建立一个Spring的配置文件,该文件就专门用来作为Spring Security的配置.使用Spring Security我们需要引入Spring Sec ...