leetcode: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.
本题是一道简单的数学题,意思就是要求两个矩形的覆盖面积。
根据每个矩形是由它的下左边角和它的上右边角定义的特征,再结合公式:覆盖面积=两个矩形的面积-相交的面积,即可。
代码如下:
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int area = (C-A)*(D-B) + (G-E)*(H-F);
if (A >= G || B >= H || C <= E || D <= F)
{
return area;
}
int top = (D>H)?H:D; //和用min(D,H)是一样的
int bottom = max(B, F);
int left = max(A, E);
int right = min(C, G);
return area - (top-bottom)*(right-left);
}
};
看了看别人做的,
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
if(A > E) return computeArea(E, F, G, H, A, B, C, D);
int res = (C - A)*(D- B) + (G - E)*(H - F);
if(C > E && B < H && F < D) res -= (min(C, G) - E) * (min(D, H) - max(B, F));
return res;
}
};
核心思想都是差不多的。
leetcode:Rectangle Area的更多相关文章
- [LeetCode] 850. Rectangle Area II 矩形面积之二
We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...
- [LeetCode] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- LeetCode之“数学”:Rectangle Area
题目链接 题目要求: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle i ...
- leetcode之Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- 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(41)-Rectangle Area
题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...
- leetcode 850. Rectangle Area II
给定一些矩形2 求覆盖面积 矩形不超过200个 1 算法1 朴素思想 虽然朴素但是代码却有意思 利用容斥原理 复杂度高达 N*2^N class Solution: def intersect(rec ...
随机推荐
- JDBC数据库连接池原理
JDBC是java数据库连接的简称.它是一种用于实行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用java语言编写的类和接口组成.其相关的API都在java.sql.*包下 ...
- Spring Mail
想必大家在今天这个特殊的日子里,一定热血沸腾了一把.为上午的阅兵点70个赞! 进入Spring Mail的主题: 前后大概花了8个小时的时间来研究Spring封装的javaMail.本来觉得挺简单的应 ...
- AD转换后数字量的处理
假设模拟输入电压的最大值为5V,A/D转换器件为8位转换. [该转换器的分辨率为1/2n=0.3906%.] [能分辨输入模拟电压变化的最小值为5*0.3906%=19.5mv.] 则模拟电压与数字输 ...
- Struct2、Hibernate3、Spring3框架搭建实战(转)
采用目前最新的struts-2.3.1.2.hibernate3.6.10.Final.spring-framework-3.1.1.RELEASE开发包,以及eclipse-jee-indigo-S ...
- AngularJs学习笔记--directive
原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...
- iptables使用multiport 添加多个不连续端口 不指定
iptables使用multiport 添加多个不连续端口 碟舞飞扬 , 01:26 , Linux技术 , 评论(0) , 引用(0) , 阅读(12214) , Via 本站原创 大 | 中 ...
- Linux 双线策略路由的三种实现方式总结+端口映射
Linux 双线策略路由的三种实现方式总结+端口映射 Linux 双线策略路由的三种实现方式总结+端口映射 网络环境 服务器(网关): eth0 为LAN口,IP为 LAN_IP = 192.168. ...
- IE6 png 透明--四种解决方法
FF和IE7已经直接支持透明的png图了,下面这个主要是解决IE6下透明PNG图片有灰底的 方法一:定义一个样式,给某个div应用这个样式后,div的透明png背景图片自动透明了.(注意两处图片的路径 ...
- github研究
一个程序猿一定会用git,但是我还没怎么用过,平时真是懒啊,学习之!...
- 编程实现Linux下的ls -l
头文件 #ifndef __FUNC_H__ #define __FUNC_H__ #include <stdio.h> #include <stdlib.h> #includ ...