LeetCode : 223. Rectangle Area
1 class Solution { 2 public: 3 int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 4 double height_A=0, width_A=0,height_B=0, width_B=0,height_OL=0, width_OL=0; 5 6 double rightBoundary = 0; 7 double leftBoundary = 0; 8 double upperBoundary = 0; 9 double lowerBoundary = 0; 10 11 height_A = D-B; 12 width_A = C-A; 13 height_B = H-F; 14 width_B = G-E; 15 16 if(C>G) rightBoundary = C; 17 else rightBoundary = G; 18 19 if(A<E) leftBoundary = A; 20 else leftBoundary = E; 21 22 if(H>D) upperBoundary = H; 23 else upperBoundary = D; 24 25 if(B<F) lowerBoundary = B; 26 else lowerBoundary = F; 27 28 height_OL = height_A+height_B-(upperBoundary-lowerBoundary); 29 width_OL = width_A+width_B-(rightBoundary-leftBoundary); 30 31 if(height_OL<0||width_OL<0){ 32 return height_A*width_A + height_B*width_B; 33 }else{ 34 return height_A*width_A + height_B*width_B - height_OL*width_OL; 35 } 36 } 37 38 };
下面这个速度会快很多:
1 class Solution { 2 public: 3 int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 4 int overlap=0; 5 //case 1: 6 if(C>=E&&C<=G&&B>=F&&B<=H && E>=A&&E<=C&&H>=B&&H<=D){ 7 overlap = (C-E)*(H-B); 8 } 9 //case 2: 10 if(C>=E&&C<=G&&B>=F&&B<=H && C>=E&&C<=G&&D>=F&&D<=H && A<=E){ 11 overlap = (C-E)*(D-B); 12 } 13 //case 3: 14 if(C>=E&&C<=G&&D>=F&&D<=H && E>=A&&E<=C&&F>=B&&F<=D){ 15 overlap = (C-E)*(D-F); 16 } 17 //case 4: 18 if(E>=A&&E<=C&&F>=B&&F<=D && E>=A&&E<=C&&H>=B&&H<=D && D>=H && B<=F){ 19 overlap = (C-E)*(H-F); 20 } 21 //case 5: 22 if(A>=E&&A<=G&&B>=F&&B<=H && C>=E&&C<=G&&B>=F&&B<=H && D>=H){ 23 overlap = (C-A)*(H-B); 24 } 25 //case 6 && case 13 && case 14: 26 if(A>=E&&A<=G&&B>=F&&B<=H && C>=E&&C<=G&&B>=F&&B<=H && A>=E&&A<=G&&D>=F&&D<=H && C>=E&&C<=G&&D>=F&&D<=H){ 27 overlap = (C-A)*(D-B); 28 } 29 //case 7: 30 if(A>=E&&A<=G&&D>=F&&D<=H && C>=E&&C<=G&&D>=F&&D<=H && B<=F){ 31 overlap = (C-A)*(D-F); 32 } 33 //case 8: 34 if(A>E&&C<=G && D>=H && B<=F){ 35 overlap = (C-A)*(H-F); 36 } 37 //case 9: 38 if(A>=E&&A<=G&&B>=F&&B<=H && G>=A&&G<=C&&H>=B&&H<=D){ 39 overlap = (G-A)*(H-B); 40 } 41 //case 10: 42 if(A>=E&&A<=G&&B>=F&&B<=H && A>=E&&A<=G&&D>=F&&D<=H && C>=G){ 43 overlap = (G-A)*(D-B); 44 } 45 //case 11: 46 if(A>=E&&A<=G&&D>=F&&D<=H && G>=A&&G<=C&&F>=B&&F<=D){ 47 overlap = (G-A)*(D-F); 48 } 49 //case 12: 50 if(G>=A&&G<=C&&H>=B&&H<=D && G>=A&&G<=C&&F>=B&&F<=D && D>=H && B<=F){ 51 overlap = (G-A)*(H-F); 52 } 53 //case 15: 54 if(E>=A&&G<=C && H>=D && F<=B){ 55 overlap = (D-B)*(G-E); 56 } 57 //case 16: 58 if(E>=A&&E<=C&&F>=B&&F<=D && G>=A&&G<=C&&F>=B&&F<=D && E>=A&&E<=C&&H>=B&&H<=D && G>=A&&G<=C&&H>=B&&H<=D){ 59 overlap = (G-E)*(H-F); 60 } 61 //case 17: 62 if(E>=A&&E<=C&&F>=B&&F<=D && G>=A&&G<=C&&F>=B&&F<=D && H>=D){ 63 overlap = (G-E)*(D-F); 64 } 65 //case 18: 66 if(E>=A&&E<=C&&H>=B&&H<=D && G>=A&&G<=C&&H>=B&&H<=D && F<=B){ 67 overlap = (G-E)*(H-B); 68 } 69 return ((C-A)*(D-B)+(G-E)*(H-F)-overlap); 70 } 71 72 };
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· 我干了两个月的大项目,开源了!
· 千万级的大表,如何做性能调优?
· 盘点!HelloGitHub 年度热门开源项目
· Phi小模型开发教程:用C#开发本地部署AI聊天工具,只需CPU,不需要GPU,3G内存就可以运行,
· 你所不知道的 C/C++ 宏知识——基于《C/C++ 宏编程的艺术》