Leetcode_223_Rectangle Area

本文是在学习中的总结。欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46868363

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.

思路:

(1)题意为给定两个矩形的对角坐标,求解两矩形所形成的面积大小。

(2)该题比較简单。最開始对其进行分析时,考虑的比較复杂。

考虑到两个矩形之间的关系无非包括三种:相离、相交、包括。

对于相离,仅仅须要判定当中一个矩形的横、纵坐标的最小值大于还有一个的最大值就可以。对于包括。仅仅需判定当中一个矩形的两个顶点坐标范围是否在还有一个矩形中就可以。对于包括,略微有一点复杂,包括当中一个矩形的一个顶点和两个顶点在还有一个矩形中的情况。对于本题,则仅仅需考虑上图所看到的的情况就可以,相对照较简单。仅仅需求出总面积。然后减去公共的面积,即为两矩形所形成的面积。

(3)详情见下方代码。希望本文对你有所帮助。

算法代码实现例如以下:

/**
 *
 * @author lqq
 *
 */
public class Rectangle_Area {

	public int computeArea(int A, int B, int C, int D, int E, int F, int G,int H) {
		//
		// //相离
		// if((E>=C || G<=A) && (H<=B || F>=D)){
		// int _area1 = (C-A>=0?(C-A):(A-C)) * (B-D>=0?

(B-D):(D-B));
		// int _area2 = (G-E>=0?(G-D):(D-G)) * (H-F>=0?(H-F):(F-H));
		//
		// return _area1 + _area2;
		// }
		//
		// //包括
		// if((A<=E && E<=C && A<=G &&G<=C&&B<=F&&F<=D&&B<=H&&H<=D)){
		// int _area1 = (C-A>=0?(C-A):(A-C)) * (B-D>=0?(B-D):(D-B));
		// return _area1;
		// }else if((A>=E && E>=C && A>=G &&G>=C&&B>=F&&F>=D&&B>=H&&H>=D)){
		// int _area2 = (G-E>=0?

(G-D):(D-G)) * (H-F>=0?

(H-F):(F-H));
		// return _area2;
		// }
		//
		// //相交 分为一个点在里面和两个点在里面

		int area = (D - B) * (C - A) + (G - E) * (H - F); // 总面积

		int left = Math.max(A, E);
		int down = Math.max(B, F);

		int right = Math.min(G, C);
		int up = Math.min(D, H);

		if (up <= down || right <= left) {
			return area;
		}

		area = area - (right - left) * (up - down);

		return area;
	}
}
时间: 2024-07-29 09:23:28

Leetcode_223_Rectangle Area的相关文章

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