首页 > 其他 > 详细

leetcode_Rectangle Area _easy

时间:2015-06-24 22:38:58      阅读:268      评论:0      收藏: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.

技术分享

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 xMin,xMax,yMin,yMax;
       int sum=(C-A)*(D-B)+(G-E)*(H-F);//两个矩形,面积的直接和
       //找到两个矩形的区域
       xMin=A>E?A:E;
       xMax=C<G?C:G;
       yMin=B>F?B:F;
       yMax=D<H?D:H;
       if(xMax>xMin && yMax>yMin)//判断两个矩形是否真的存在此相交区域
         sum-=(xMax-xMin)*(yMax-yMin);//存在,则减去相交区域面积
       return sum;
    }
};




leetcode_Rectangle Area _easy

原文:http://blog.csdn.net/u013861066/article/details/46625741

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!