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. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  2. ECSHOP在线手册布局参考图--通用活动页 group_list.dwt等

        A.购物车 1,设置方法 程序自动读取购物车的商品数量 2,代码相关 cart.lbi 中 {insert_scripts files='transport.js'} <div clas ...

  3. spring aop 的一个demo(未完,待完善)

    假设我们有这样的一个场景 : 对于一个类的众多方法,有些方法需要从缓存读取数据,有些则需要直接从数据库读取数据.怎样实现呢? 实现方案有多种.下面我说下常见的几种实现方案 : 1.直接采用spring ...

  4. hadoop 1.2 集群搭建与环境配置

    一.虚拟机环境 见我的另一篇博客http://www.cnblogs.com/xckk/p/6000881.html, 需要安装JDK环境,centos下安装JDK可参考: http://www.ce ...

  5. 使用JDK自带jvisualvm监控tomcat

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. 【转】linux中的sort命令

    转自:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分 ...

  7. 7 种流行 PHP IDE 的比较

    编写关于 PHP 的系列文章让我更加深刻地了解了 PHP 开发人员的世界.我和许多 PHP 程序员交谈过,最令我惊奇的是只有很少的人使用 IDE.大多数程序员使用文本编辑器,比如 Microsoft® ...

  8. CustomViewWith_Image_Text_Video

    CustomViewOfTextVideoImage.rar https://github.com/Grishu/CustomViewWith_Image_Text_Video

  9. 基于Android 平台简易即时通讯的研究与设计[转]

    摘要:论文简单介绍Android 平台的特性,主要阐述了基于Android 平台简易即时通讯(IM)的作用和功能以及实现方法.(复杂的通讯如引入视频音频等可以考虑AnyChat SDK~)关键词:An ...

  10. [MODx] 8. Snippet get data, chunk display

    Simple Example: Lets process this chunk and output its value. We have this Chunk, called "Welco ...