[223]Rectangle Area
来源:互联网 发布:淘宝直播账号出售 编辑:程序博客网 时间:2024/05/16 17:13
【题目描述】
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.
分类讨论,寻找规律
【代码】
class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int S1=(C-A)*(D-B); int S2=(G-E)*(H-F); if(C<=E||G<=A||D<=F||H<=B) return S1+S2; int m[4],n[4]; m[0]=A; m[1]=C; m[2]=E; m[3]=G; n[0]=B; n[1]=D; n[2]=F; n[3]=H; sort(m,m+4); sort(n,n+4); int ans=S1+S2-(m[2]-m[1])*(n[2]-n[1]); return ans; }};
改进版本:
class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int S1=(C-A)*(D-B); int S2=(G-E)*(H-F); if(C<=E||G<=A||D<=F||H<=B) return S1+S2; int ans=S1+S2-(min(G,C)-max(A,E))*(min(H,D)-max(B,F)); return ans; }};
0 0
- Rectangle Area - LeetCode 223
- leetcode 223: Rectangle Area
- leetcode[223]:Rectangle Area
- Leetcode 223 Rectangle Area
- 223Rectangle Area
- leetcode 223:Rectangle Area
- leetcode-223 Rectangle Area
- 223 Rectangle Area
- Leetcode #223 Rectangle Area
- leetCode #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
- LeetCode 223 Rectangle Area
- LeetCode 223:Rectangle Area
- CSS设置DIV居中
- jquery简单动画效果(上)
- printf输出各种格式(转)
- Android学习历程之【安装】
- foj2155 - 盟国 (并查集的删除操作)
- [223]Rectangle Area
- 讨论
- 测试二知识总结
- HDU2642 Stars 二维树状数组
- php UNSIGNED ZEROFILL
- 手游创业漫话五 (我要做什么游戏 下)
- Win7平台VS2010安装Visual Assist X
- 避免带有变长参数的方法重载,别让null值和空值威胁到变长方法,覆写变长方法也循规蹈矩
- 手游创业漫话六(有效率的公司组织 上)