【Leetcode】223. Rectangle Area(计算两矩形覆盖总面积) 深藏阁楼爱情的钟 2022-01-29 18:23 182阅读 0赞 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][] **Example:** Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2 Output: 45 解题思路: 左侧的最大值为left,右侧为最小值的坐标并且与left进行比较找出最大值。 同理可得top,bottom class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int left = max(A,E), right = max(min(C,G), left); int bottom = max(B,F), top = max(min(D,H), bottom); return (C-A)*(D-B) - (right-left)*(top-bottom) + (G-E)*(H-F); } }; [Rectangle Area]: https://assets.leetcode.com/uploads/2018/10/22/rectangle_area.png
还没有评论,来说两句吧...