关闭

【leetcode】223. Rectangle Area

标签: leetcode
178人阅读 评论(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

Assume that the total area is never beyond the maximum possible value of int.


题目解读:求两个矩阵的总面积(即两个矩阵面积 - 覆盖的面积)


思路:分情况讨论即可


c++代码(32ms,56.67%)

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        if(C<E || G<A || H<B || D<F)
            return (C-A)*(D-B)+(G-E)*(H-F);
        else{
            //算出长和高 
            int length,height; //长,高
            if(A<=E && E<=C && C<=G)
                length=C-E;
            else if(A<=E && E<=G && G<=C)
                length=G-E;
            else if(E<=A && A<=G && G<=C)
                length=G-A;
            else 
                length=C-A;
            
            if(D>=H && H>=B && B>=F)
                height=H-B;
            else if(D>=H && H>=F && F>=B)
                height=H-F;
            else if(H>=D && D>=F && F>=B)
                height=D-F;
            else
                height=D-B;
                
            return (C-A)*(D-B)+(G-E)*(H-F)-length*height;
    
        }
            
    }
};


更简洁的代码

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);
}



0
0
查看评论

Leetcode_223_Rectangle Area

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46868363 Find the total area covered by two rectilinear rectangles i...
  • pistolove
  • pistolove
  • 2015-07-13 22:02
  • 989

leetcode 223: Rectangle Area

leetcode 223: Rectangle Area java c++ python
  • xudli
  • xudli
  • 2015-06-10 03:25
  • 8330

【LeetCode-面试算法经典-Java实现】【223-Rectangle Area(矩形区域)】

【223-Rectangle Area(矩形区域)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao】原题  Find the total area covered by two rectilinear r...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-29 06:39
  • 2714

【LeetCode-223】 Rectangle Area(C++)

题目要求:求两个矩形所围成的总面积,每个矩形均由左下角的点坐标和右上点的坐标定义。如下图: 解题思路:其实就是简单的数学问题,首先判断两矩阵是不是相交,如果不相交,直接将两矩阵的面积相加;如果相交,则将两矩阵面积相加在减去公共区域的面积。 代码实现:class Solution { pu...
  • liujiayu1015
  • liujiayu1015
  • 2016-05-11 22:20
  • 218

[leetcode-223]Rectangle Area(c++)

问题描述: 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 corne...
  • zdavb
  • zdavb
  • 2015-08-25 10:09
  • 356

LeetCode - 223. Rectangle Area

这道题目的思路比较直接,根据以前做数学题的经验,首先计算出两个长方形的面积,然后进行是否重叠的判断,题目里的重点就在于重叠的判断,判断的方式如下,也比较容易理解: int left = Math.max(A, E); int right = Math.min(C, G); int bottom =...
  • shenzhu0127
  • shenzhu0127
  • 2016-07-17 21:35
  • 147

【LeetCode】 223. Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner an...
  • qq_31129455
  • qq_31129455
  • 2016-12-23 06:17
  • 78

223. Rectangle Area LeetCode

题意:给出两个矩形的左下角坐标和右上角坐标。问两个矩形并的面积。 题解:用两个矩形面积减去两个矩形并的面积就可以。class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G...
  • youthinkwu
  • youthinkwu
  • 2016-02-12 12:34
  • 163

[Leetcode]223. Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner an...
  • sunhero2010
  • sunhero2010
  • 2016-07-13 19:49
  • 123

LeetCode : 223. Rectangle Area

问题描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom lef...
  • u014221279
  • u014221279
  • 2016-08-30 16:02
  • 197
    个人资料
    • 访问:223719次
    • 积分:4285
    • 等级:
    • 排名:第8444名
    • 原创:230篇
    • 转载:0篇
    • 译文:0篇
    • 评论:76条
    最新评论