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
随机推荐
- iOS CFStringTransForm汉字转拼音
CFStringTransform - (NSString *) phonetic:(NSString*)sourceString { NSMutableString *source = [sourc ...
- LINQ里的“equals”和“==”的区别
对于值类型,如果对象的值相等,则相等运算符 (==) 返回 true,否则返回 false.对于string 以外的引用类型,如果两个对象引用同一个对象,则 == 返回 true.对于 string ...
- radio的选中设置以及取值。
前台:<input type=" id="tg" name="state"/> <a style="cursor:poin ...
- 印象烟大PPT大赛
下面为获奖人员 王志恒一等奖 姜云飞.任子仪二等奖 田正相,庄棫麟,陈德昊三等奖.
- Ubuntu安装Mysql过程及远程问题解决
ubuntu下执行 sudo apt-get instlll mysql-server sudo apt-get install mysql-client 安装过程中会有文字界面设置密码 牢记密码 M ...
- 老oj1965:polygon半平面交
题目链接:http://192.168.2.240:8080/JudgeOnline/showproblem?problem_id=1965 polygon半平面交 Time Limit:1000MS ...
- Traffic Ccontrol(流量控制)
linux有一个成熟的带宽供给系统,称为Traffic Control(流量控制).这个系统支持各种方式进行分类.排序.共享和限制出入流量. 一.基础知识 让ip显示我们的链路 ip link li ...
- Integer.valueOf(int)及自动装箱内幕
Integer为什么要提供功能与new Integer(xx)一样的valueOf(xx)方法呢,看了源代码之后,我发现了惊人的内幕. public static Integer valueOf(in ...
- 判断括号字符串是否为合法+求n对括号的所有组合
n对括号的有效组合数 参考:https://zh.wikipedia.org/wiki/%E5%8D%A1%E5%A1%94%E5%85%B0%E6%95%B0 import java.util.Ar ...
- linode digitalocean哪个更好
大多数人纠结的品牌是Linode和DigitalOcean.我有幸使用过两者的产品,从2011年起,我就在用Linode VPS套餐,2013年开始,我订购了一批DigitalOcean产品,下面说下 ...