标签:假设 pre nbsp http leetcode amp lse ems com
在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积。
每个矩形由其左下顶点和右上顶点坐标表示,如图所示。
示例:
输入: -3, 0, 3, 4, 0, -1, 9, 2
输出: 45
说明: 假设矩形面积不会超出 int 的范围。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rectangle-area
class Solution { public: int computeArea(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { if(x1>x2)swap(x1,x2); if(y1>y2)swap(y1,y2); if(x3>x4)swap(x3,x4); if(y3>y4)swap(y3,y4); int num=0; int x=0; if(x1<=x3&&x4<=x2)x=x4-x3; else if(x3<=x1&&x2<=x4)x=x2-x1; else if(x2<=x4&&x3<=x2)x=x2-x1+x4-x3-(x4-x1); else if(x3<=x1&&x1<=x4)x=x2-x1+x4-x3-(x2-x3); int y=0; if(y1<=y3&&y4<=y2)y=y4-y3; else if(y3<=y1&&y2<=y4)y=y2-y1; else if(y2<=y4&&y3<=y2)y=y2-y1+y4-y3-(y4-y1); else if(y3<=y1&&y1<=y4)y=y2-y1+y4-y3-(y2-y3); return (x2-x1)*(y2-y1)-x*y+(x4-x3)*(y4-y3); } };
标签:假设 pre nbsp http leetcode amp lse ems com
原文地址:https://www.cnblogs.com/wz-archer/p/12590091.html