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.
- 得到两个长方形的最大覆盖面积;主要是处理重叠部分
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相关推荐
- LeetCode——223. 矩形面积(Rectangle Area)[中等]——分析及代码(C++)
LeetCode--223. 矩形面积[Rectangle Area][中等]--分析及代码[C++] 一.题目 二.分析及代码 1. 几何计算 (1)思路 (2)代码 (3)结果 三.其他 一.题目 ...
- LeetCode 850. Rectangle Area II
题目地址: We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [x1, y1, x2, y2] , where ...
- 223. Rectangle Area
题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...
- Leetcode题目:Rectangle Area
题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...
- leetcode 850. Rectangle Area II | 850. 矩形面积 II(递归分割未重叠矩形)
题目 https://leetcode.com/problems/rectangle-area-ii/ 题解 没有看懂官方答案,评论区有一种解法写的挺通俗的: Clean Recursive Solu ...
- 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 ...
- LeetCode Rectangle Area
题意:给出两个矩形,求其面积和 思路:两个矩形可能相交,在求相交面积时,左下角取两个矩形左下角的最大值 ,右上角取两个矩形对应坐标的最小值 ,如果求得的左下角大于右下角,说明不相交,面积为两个矩形面积 ...
- leetcode 223. Rectangle Area | 223. 矩形面积(Java)
题目 https://leetcode.com/problems/rectangle-area/ 类似问题:蓝桥杯 BASIC-18 基础练习 矩形面积交 题解 本题的重点在于如何计算重叠部分的面积, ...
- 定义矩形类(长length,宽widen)求面积(area)(简单例子)
#include<iostream> using namespace std; class Rectangle{ ...
最新文章
- android 加载多个图片 内在溢出的问题
- POJ-2976 Dropping tests 01分数规划
- centos下添加windows启动项
- 【嵌入式Linux】嵌入式Linux驱动开发基础知识之LED驱动框架--面向对象、分层设计思想
- 【LeetCode】【HOT】208. 实现 Trie (前缀树)
- 62. 拆分初始化负载
- 中学办公室计算机管理制度,中学多媒体室使用管理制度条例
- Mysql的可视化工具整理
- 《摩根写给儿子的32封信》 03 企业家的资质
- 继微博之后,.fans建站成饭圈新宠
- Android Studio Text组件介绍
- 商人过河c语言编程,商人过河问题C语言源码.c
- babel-plugin-transform-remove-consol插件的安装及使用(作用是移除代码里的所有console.log())
- html 气泡上升效果,css气泡效果
- 厉害了,分布式数据库中间件ShardingSphere毕业成为Apache顶级项目!
- 小白兔写话_看图写话小白兔
- Google Chrome 浏览器常用快捷键
- 易语言如何调用大漠插件
- 用友开发者社区 - http://udn.yonyou.com
- python menuconfig_如何配置 ESP32 Menuconfig