首页 > 学院 > 开发设计 > 正文

223. Rectangle Area

2019-11-08 02:08:16
字体:
来源:转载
供稿:网友

题目

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.

Credits: Special thanks to @mithmatt for adding this PRoblem, creating the above image and all test cases.

Subscribe to see which companies asked this question.


思路

要计算的面积是两者各自的面积减去覆盖的面积


代码

class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { //思路:两者面积和减去覆盖的面积 int areaA = (C-A)*(D-B); int areaB = (G-E)*(H-F); int left = max(A,E); int right = min(C,G); int bottom = max(B,F); int top = min(D,H); int overloopArea = 0; if(right > left && top > bottom) { overloopArea = (right-left)*(top-bottom); } return areaA+areaB-overloopArea; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表