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.
这道题目,在leetcode上有很多所谓“一句话就实现”的代码。不过我觉得那些代码的可读性太差。下面是我写的代码:
#include<stdio.h>
// 计算两条线段重合的部分,如果没有重合的部分,则输出0
// 以X轴为示例,先找到两条线段左侧坐标的最大值
// 再找到两条线段右侧坐标的最小值
// 然后用右侧坐标的最小值减去左侧坐标的最大值
// 如果相减得到的值小于0,则表示没有重合
// 如果相减得到的值大于等于0,则表示有重合
// 相减得到的值即为重合部分的长度
int coverLength(int A, int C, int E, int G){
int maxA = ;
if (A > E){
maxA = A;
}
else{
maxA = E;
} int minC = ;
if (C < G){
minC = C;
}
else{
minC = G;
} int outInt = minC - maxA;
if (outInt < ){
return ;
}
return outInt;
} int computeRetangleArea(int A, int B, int C, int D){
return (C - A)* (D - B);
}
// 计算出两个矩形的面积之和,然后再减去重合部分的面积
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H){
int x = coverLength(A, C, E, G);
int y = coverLength(B, D, F, H);
int coverArea = x*y;
int firstArea = computeRetangleArea(A, B, C, D);
int lastArea = computeRetangleArea(E, F, G, H);
return firstArea + lastArea - coverArea;
} void main(){
int area = computeArea(, , , , -, -, , );
printf("%d\n", area);
}
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 ...
- 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 ...
- leetcode: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 ...
- LeetCode : 223. Rectangle Area
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
随机推荐
- iOS 沙盒目录结构介绍
iOS系统中,每个应用都有自己的沙盒,且应用只能访问其对应的沙盒目录下面的文件.当然,在用户授权的情况下,应用也可以访问其他目录下面的文件.比如,用户授权情况下,应用可以访问相册.通讯录.在开发中,经 ...
- java异步任务处理
1.场景 最近做项目的时候遇到了一个小问题:从前台提交到服务端A,A调用服务端B处理超时,原因是前端一次请求往db插1万数据,插完之后会去清理缓存.发送消息. 服务端的有三个操作 a.插DB b.清理 ...
- linux 常用命令 -- 系统管理工具包: 监视邮件的使用情况
清单 5. 获得磁盘使用情况统计信息 $ du -sk * 20 admin 1020 appleby 45828 applicants 13264 buy 11704 dev 11200 finan ...
- MySQL5日期类型DATETIME和TIMESTAMP相关问题详解
MySQL5日期类型DATETIME和TIMESTAMP相关问题详解 MySQL5的日期类型有三种:DATETIME.DATE和TIMESTAMP,除了DATE用来表示一个不带时分秒的是日期,另外两个 ...
- HTML5----CSS显示半个字符
CSS显示半个字符的基本思路: 就是一个字写两遍,分别显示一半.这里就须要用到CSS伪元素:before和:after,记住这个"伪元素"的"伪"字,表明它本来 ...
- android126 zhihuibeijing 极光推送
https://www.jpush.cn/ 张三把消息发送给自己的服务器,自己的服务器将消息发送给极光推送,然后极光推送将消息发送给妹子. 清单文件: <?xml version="1 ...
- C 栈顺序存储
// seqstack.h #ifndef _MY_SEQSTACK_H_ #define _MY_SEQSTACK_H_ typedef void SeqStack; SeqStack* SeqSt ...
- [原创]VS2013 EF6连接MySql需要几步?
精简的美丽...... 1.安装mysql server下载地址 http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.21-winx64.zip注意: ...
- 慎用StringEscapeUtils.escapeHtml步骤
慎用StringEscapeUtils.escapeHtml方法[转] 推荐使用Apache commons-lang的StringUtils来增强Java字符串处理功能,也一直在项目中大量使用Str ...
- 【转】使用Beaglebone Black的I2C (二)——使用C语言和i2c-dev驱动
在本博客的<使用Beaglebone Black的I2C(一)>中,介绍了BBB上无需编程对i2c总线进行读写操作的方法,本文将介绍如何在c语言程序中使用i2c-dev驱动来操作i2c设备 ...