leetcode_Rectangle Area _easy

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;
    }
};
时间: 2024-11-05 18:55:39

leetcode_Rectangle Area _easy的相关文章

POJ 2546 &amp; ZOJ 1597 Circular Area 两圆的面积交

Circular Area Time Limit: 2 Seconds      Memory Limit: 65536 KB Your task is to write a program, which, given two circles, calculates the area of their intersection with the accuracy of three digits after decimal point. Input In the single line of in

map area (append)

<html><body><script src="http://code.jquery.com/jquery-1.11.0.min.js"></script><!--<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>--><script >    $(function(){ 

使用area标签模仿a标签

众所周知,map标签可以给img图像标记热点区域,而area标签就是跟map标签一起使用的. 但area标签的作用可不止用来标记热点,因为它也有href属性,因此某些场景可以代替a标签进行页面跳转. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style ty

HDU1071 The area

Ignatius bought a land last week, but he didn't know the area of the land because the land is enclosed by a parabola and a straight line. The picture below shows the area. Now given all the intersectant points shows in the picture, can you tell Ignat

25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有

package zhongqiuzuoye; public class Rect { public double width; public double height; Rect(double width,double height) //带有两个参数的构造方法,用于将width和height属性初化; { this.width=width; this.height=height; } Rect() //不带参数的构造方法,将矩形初始化为宽和高都为10. { width=10; height=

Broadmann area (wiki)

Sources: https://en.wikipedia.org/wiki/Brodmann_area Lateral surface Medial serface Areas 3, 1 & 2 – Primary Somatosensory Cortex (frequently referred to as Areas 3, 1, 2 by convention) Area 4 – Primary Motor Cortex Area 5 – Somatosensory Association

[UVA Live 12931 Common Area]扫描线

题意:判断两个多边形是否有面积大于0的公共部分 思路:扫描线基础. #pragma comment(linker, "/STACK:10240000") #include <bits/stdc++.h> using namespace std; #define X first #define Y second #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #de

LeetCode:Rectangle Area - 矩形交叉部分的面积

1.题目名称 Rectangle Area(矩形交叉部分的面积) 2.题目地址 https://leetcode.com/problems/rectangle-area/ 3.题目内容 英文: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

hdu 1071 - The area(解题报告)

The area Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8715    Accepted Submission(s): 6115 Problem Description Ignatius bought a land last week, but he didn't know the area of the land becau