223. Rectangle Area
来源:互联网 发布:benq显示器怎么样 知乎 编辑:程序博客网 时间:2024/06/15 16:50
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.
Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.
s思路:
1. 这道题,就是纯数学题了。通过比较坐标、坐标运算,得到相交的长方形的区间。如下图,画了四种可能的情况,代码的过程不可能去讨论每一种情况,需要知道这些情况表面不一样,但是看我们站在什么角度看了:如果只看到坐标分布不同,相对位置关系不同,那么问题就千变万化;但只要能把相对关系用数学表示出来,比如:两个正方形的左边线的最大值L1,两个正方形的右边线的最小值L4,两个正方形的上边线的最小值L3,两个正方形的下边线的最大值L2,我们根据比较关系,其实就得到了相交长方形的四条边了。现在判断这四条边的关系,如果L1 < L4&&L2 < L3,那就肯定能围成长方形。
2.看起来有很多不同的情况,但通过求最大最小值,通过比较大小,所有问题都是一个问题,所有情况就是一种情况,比如:无论如何位置,无论坐标如何,只要相交都有四条边,去计算四条边,然后比较这四条边的相对位置判断是否overlap!
class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { // int l1=max(A,E),l2=max(B,F),l3=min(D,H),l4=min(C,G); int two=(C-A)*(D-B)+(G-E)*(H-F); int overlap=0; if(l1<l4&&l2<l3) overlap=(l4-l1)*(l3-l2); return two-overlap; }};
0 0
- 223. Rectangle Area
- [leetcode] 223.Rectangle Area
- [leetcode] 223. Rectangle Area
- 223. Rectangle Area LeetCode
- #leetcode# 223. Rectangle Area
- 223. Rectangle Area
- leetcode 223. Rectangle Area
- 223. Rectangle Area
- LeetCode--223. Rectangle Area
- 223. Rectangle Area
- LeetCode *** 223. Rectangle Area
- LeetCode 223. Rectangle Area
- leetcode-223. Rectangle Area
- 223. Rectangle Area
- 223. Rectangle Area
- LeetCode-223.Rectangle Area
- 223.[Leetcode]Rectangle Area
- 223. Rectangle Area
- Could not set property 'xxxx' of 'class 'xxxxxxx' with value 'xxxxxx'
- 1IntelliSense: #error 指令: Please use the /MD switch for _AFXDLL buildsc:\program files\microsoft
- 动规-矩阵快速幂
- 匿名对象不同的jdk5新特性
- 加速加速boo!
- 223. Rectangle Area
- js原型继承中setter的行为
- 微积分新旧之争,不是纯学术之争,而是具有深刻的社会背景
- Odoo Module (一)
- 226. Invert Binary Tree
- 完美解决Windows7安装VC6后无法创建新项目的问题
- Secure CRT 自动记录日志和时间戳功能配置
- 蓝桥杯(算法训练)——K好数
- Python轻松入门-13 函数返回值