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.

  • 得到两个长方形的最大覆盖面积;主要是处理重叠部分
void swapTwo(int *A, int *B)
{int tmp = *A;*A = *B;*B = tmp;
}
void sortFour(int *A, int *C, int *E, int *G)
{int tmp = 0;if(*A > *C)swapTwo(A, C);if(*E > *G)swapTwo(E, G);if(*A > *E)swapTwo(A, E);if(*C > *E)swapTwo(C, E);if(*E > *G)swapTwo(E, G);
}
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {int total = (C - A) * (D - B) + (G - E) * (H - F);if(A >= G || C <= E || B >= H || D <= F)return total;else{sortFour(&A, &C, &E, &G);sortFour(&B, &D, &F, &H);return total - (E - C) * (F - D);}
}

  • 如果存在重叠部分,则把横纵各4个坐标排序,得到中间两个,然后得到重叠部分的面积
int max(int a, int b)
{if(a > b)return a;else return b;
}
int min(int a, int b)
{if(a > b)return b;elsereturn a;
}
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {int total = (C - A) * (D - B) + (G - E) * (H - F);if(A >= G || C <= E || B >= H || D <= F)return total;int top = min(D, H);int bottom = max(B, F);int left = max(A, E);int right = min(C, G);return total - (top - bottom) * (right - left);
}

  • 看起来比上面的代码简单,可是速度竟然没有加快;

转载于:https://www.cnblogs.com/dylqt/p/4908217.html

Rectangle Area相关推荐

  1. LeetCode——223. 矩形面积(Rectangle Area)[中等]——分析及代码(C++)

    LeetCode--223. 矩形面积[Rectangle Area][中等]--分析及代码[C++] 一.题目 二.分析及代码 1. 几何计算 (1)思路 (2)代码 (3)结果 三.其他 一.题目 ...

  2. LeetCode 850. Rectangle Area II

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

  3. 223. Rectangle Area

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

  4. Leetcode题目:Rectangle Area

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

  5. leetcode 850. Rectangle Area II | 850. 矩形面积 II(递归分割未重叠矩形)

    题目 https://leetcode.com/problems/rectangle-area-ii/ 题解 没有看懂官方答案,评论区有一种解法写的挺通俗的: Clean Recursive Solu ...

  6. Leet Code OJ 223. Rectangle Area [Difficulty: Easy]

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

  7. LeetCode Rectangle Area

    题意:给出两个矩形,求其面积和 思路:两个矩形可能相交,在求相交面积时,左下角取两个矩形左下角的最大值 ,右上角取两个矩形对应坐标的最小值 ,如果求得的左下角大于右下角,说明不相交,面积为两个矩形面积 ...

  8. leetcode 223. Rectangle Area | 223. 矩形面积(Java)

    题目 https://leetcode.com/problems/rectangle-area/ 类似问题:蓝桥杯 BASIC-18 基础练习 矩形面积交 题解 本题的重点在于如何计算重叠部分的面积, ...

  9. 定义矩形类(长length,宽widen)求面积(area)(简单例子)

    #include<iostream> using namespace std; class Rectangle{                                       ...

最新文章

  1. android 加载多个图片 内在溢出的问题
  2. POJ-2976 Dropping tests 01分数规划
  3. centos下添加windows启动项
  4. 【嵌入式Linux】嵌入式Linux驱动开发基础知识之LED驱动框架--面向对象、分层设计思想
  5. 【LeetCode】【HOT】208. 实现 Trie (前缀树)
  6. 62. 拆分初始化负载
  7. 中学办公室计算机管理制度,中学多媒体室使用管理制度条例
  8. Mysql的可视化工具整理
  9. 《摩根写给儿子的32封信》 03 企业家的资质
  10. 继微博之后,.fans建站成饭圈新宠
  11. Android Studio Text组件介绍
  12. 商人过河c语言编程,商人过河问题C语言源码.c
  13. babel-plugin-transform-remove-consol插件的安装及使用(作用是移除代码里的所有console.log())
  14. html 气泡上升效果,css气泡效果
  15. 厉害了,分布式数据库中间件ShardingSphere毕业成为Apache顶级项目!
  16. 小白兔写话_看图写话小白兔
  17. Google Chrome 浏览器常用快捷键
  18. 易语言如何调用大漠插件
  19. 用友开发者社区 - http://udn.yonyou.com
  20. python menuconfig_如何配置 ESP32 Menuconfig

热门文章

  1. iOS开发线程同步技术-锁
  2. Ubuntu16.04 使用sudo cat EOF 编辑文件,提示Permission denied错误的解决办法
  3. BZOJ 2039: [2009国家集训队]employ人员雇佣
  4. 项目开发环境(h5+pc的开发思路是一样的)
  5. 更适合程序员使用的Vim配置 显示行号 语法高亮 智能缩进
  6. 【转】找到 MySQL 数据库中的不良索引
  7. Wpf体系结构的学习
  8. Linux下 数据文件 效验问题
  9. Python--day63--添加书籍
  10. 【避坑】初次接项目的血与泪,扎坑了老