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
随机推荐
- 编写Qt Designer自定义控件(二)——编写自定义控件界面
接上文:编写Qt Designer自定义控件(一)——如何创建并使用Qt自定义控件 既然是控件,就应该有界面,默认生成的控件类只是一个继承了QWidget的类,如下: #ifndef LOGLATED ...
- Protobuf-net学习笔记
对于Socket应用来说,如何序列化和反序列化消息一直是比较头痛的问题,C#提供了自动序列化的功能(类似AS3中的AMF),但是唯一的缺点就是前后端都必须是C#实现,如果前后端语言不一致该怎么办? G ...
- java 学习基础学习单词及java关键词
在JAVA学习中我们难免会犯一些逻辑错误,语法错误,和一些运行错误,对于英语不好的人,就的记下下面的2常用单词,有助于我们提高在使用软件编写代码的速度和代码调试,能更便捷的找出错误,知道1中的保溜关键 ...
- cocos2d_随手篇1_关于ccTouchBegan的调用
在新的cocos框架里,旧的调用ccTouchBegan方法被和谐掉了!so! 直接来代码 1 -(void)doSometing{ 2 [[[CCDirector sharedDirector] ...
- cocos2d-x 动画加速与减速
转自:http://novacreo.com/%E7%A8%8B%E5%BA%8F%E7%BB%84/cocos2d-x%E5%8A%A8%E7%94%BB%E5%8A%A0%E9%80%9F%E4% ...
- iOS- iPad UIPopoverController
在IPAD开发中,有一个很有趣的视图控制器,UIPopoverControllr,它的初始化必须要设置一个"内容视图",相当于它本身只是作为一个“容器”,而显示的内容还需要另外一个 ...
- C#-设置label的字体颜色和大小
在进行label的设置的过程中,常常会遇到需要设定label的字体颜色和字体的大小,这就需要用到label的属性:
- linux下web压力测试工具ab使用及详解
APACHE自带的测试工具AB(apache benchmark).在APACHE的bin目录下.格式: ./ab [options] [http://]hostname[:port]/path参数: ...
- iOS开发——数据持久化Swift篇&模型对象归档
模型对象归档 import UIKit class ViewController: UIViewController { @IBOutlet weak var textField: UITextFie ...
- android报错及解决2--Sdcard进行文件的读写操作报的异常
报错描述: 对Sdcard进行文件的读写操作的时候,报java.io.FileNotFoundException: /sdcard/testsd.txt (Permission denied),在往S ...