码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Rectangle Area

时间:2016-03-30 06:54:01      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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_AD=(C-A)*(D-B);
        int area_EH=(G-E)*(H-F);
        int max_AE=(A>E)?A:E;
        int max_BF=(B>F)?B:F;
        int min_CG=(C>G)?G:C;
        int min_DH=(D>H)?H:D;
        if(C<=E||D<=F||G<=A||H<=B)
        return area_AD+area_EH;
        else
        return area_AD+area_EH-(min_CG-max_AE)*(min_DH-max_BF);
    }
};

 

LeetCode Rectangle Area

标签:

原文地址:http://www.cnblogs.com/csudanli/p/5335558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!