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的更多相关文章

  1. [LeetCode] 850. Rectangle Area II 矩形面积之二

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...

  2. [LeetCode] 223. Rectangle Area 矩形面积

    Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...

  3. Java for LeetCode 223 Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  4. (easy)LeetCode 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  5. leetcode:Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  6. Java [Leetcode 223]Rectangle Area

    题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...

  7. LeetCode(41)-Rectangle Area

    题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...

  8. leetcode 850. Rectangle Area II

    给定一些矩形2 求覆盖面积 矩形不超过200个 1 算法1 朴素思想 虽然朴素但是代码却有意思 利用容斥原理 复杂度高达 N*2^N class Solution: def intersect(rec ...

  9. LeetCode : 223. Rectangle Area

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

随机推荐

  1. Applied Functional Analysis(Applications to Mathematical Physics ) E.Zeidler

     Applied Functional Analysis(Applications to Mathematical Physics )   E.Zeidler More: QQ565055403

  2. java.lang.UnsupportedClassVersionError: Bad version number in .class file 解决办法

    java.lang.UnsupportedClassVersionError: Bad version number in .class file 造成这种错误的原因是支撑Tomcat运行的JDK版本 ...

  3. ThinkPHP3.1快速入门(12)自动验证

    自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 验证规则 数据验证可以进行数据类型.业务规则.安全判断等方面的验证操作.数据验证有两 ...

  4. .NET连接MySql数据库的方法及示例

    方法一: 使用MySQL推出的MySQL Connector/Net组件, 该组件是MySQL为ADO.NET访问MySQL数据库设计的.NET专用访问组件.完成该组件后,需要在项目中引用这个组件,也 ...

  5. 并查集类的c++封装,比較union_find algorithm四种实现方法之间的性能区别

    问题描写叙述: 在计算机科学中,并查集是一种树型的数据结构,其保持着用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.有一个联合-查找算法(union-find algorithm ...

  6. 获取设置dom属性

    getAttribute():获取dom节点属性,带一个参数,表示要获取的属性使用方法:object.getAttribute("id"); setAttribute():设置do ...

  7. C#_delegate - 异步调用实例 BeginInvoke EndInvoke event

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. SOAP 及其安全控制--转载

    原文地址:http://my.oschina.net/huangyong/blog/287791 目录[-] 1. 基于用户令牌的身份认证 2. 基于数字签名的身份认证 3. SOAP 消息的加密与解 ...

  9. python--字符工厂函数dict()

    字符工厂函数str() class str(object): """ str(object='') -> str str(bytes_or_buffer[, enc ...

  10. Markdown基础语法

    Markdown 的优点如下: 纯文本,所以兼容性极强,可以用所有文本编辑器打开. 让你专注于文字而不是排版. 格式转换方便,Markdown 的文本你可以轻松转换为 html.电子书等. Markd ...