devops 信创在数字经济时代提升企业竞争力的关键策略
650
2022-09-04
223. 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.
class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int area = (D - B) * (C - A) + (G - E) * (H - F); // 总面积 int left = Math.max(A, E); int down = Math.max(B, F); int right = Math.min(G, C); int up = Math.min(D, H); if (up <= down || right <= left) { return area; } area = area - (right - left) * (up - down); return area; } }
class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { long left = Math.max(A, E); long right = Math.min(C, G); long top = Math.min(D, H); long bottom = Math.max(B, F); long area = (right - left) * (top - bottom); // if not overlaping, either of these two will be non-posittive // if right - left = 0, are will automtically be 0 as well if(right - left < 0 || top - bottom < 0) area = 0; return (int)((C-A) * (D-B) + (G-E) * (H-F) - area ); }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~