关闭

leetcode-223 Rectangle Area

264人阅读 评论(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.

题目意思清楚,但是要注意特殊情况,就是不相交的特例。
经过观察发现,只要满足当一个矩形右上角的横纵坐标只要有一个小于另外一个矩形左下角的横纵坐标,那么就表示两个矩形不相交。其余情况,矩形相交,计算相交矩形坐标,减去面积即可。
时间36ms。
代码如下:

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int s1=0, s2=0, s=0;
        int  s3=0 ;
        s1 = (C-A)*(D-B);
        s2 = (G-E)*(H-F);
        if(A >= G || B >= H || E >= C || F >= D)
        s3=0;
        else
        {
        int I, J , K , L;
        I = A > E ? A : E;
        J = B > F ? B : F;
        K = C < G ? C : G;
        L = D < H ? D : H;
        s3 = (K-I)*(L-J);
        }
        s = s1+s2-s3;
        return s;
    }
};
0
0
查看评论

[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(C++)

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

[LintCode]Rectangle Area(Java)

public class Rectangle {     /*      * Define two public attributes width and height of type int.      */ ...
  • gamesluo
  • gamesluo
  • 2017-08-05 15:19
  • 331

leetcode 223: Rectangle Area

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

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
  • 987

leetcode_question_85 Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectang...
  • doc_sgl
  • doc_sgl
  • 2013-09-18 14:00
  • 10011

【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
  • 2711

Largest Rectangle in Histogram -- LeetCode

原题链接: http://oj.leetcode.com/problems/largest-rectangle-in-histogram/  这道题brute force的方法很直接,就是对于每一个窗口,找到其中最低的高度,然后求面积,去其中最大的矩形面积。总共有n^2个窗口,找...
  • linhuanmars
  • linhuanmars
  • 2014-03-05 08:52
  • 15482

Rectangle Area

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...
  • brucehb
  • brucehb
  • 2015-06-08 02:07
  • 1618

CF 596A. Wilbur and Swimming Pool 【计算矩形面积】

A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input standard input output stand...
  • liu6886
  • liu6886
  • 2016-03-17 16:27
  • 342
    个人资料
    • 访问:48604次
    • 积分:1137
    • 等级:
    • 排名:千里之外
    • 原创:68篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    文章分类