"223-rectangle Area (rectangular region)"
" leetcode-interview algorithm classic-java Implementation" "All topic Directory Index"
code Download "Https://github.com/Wang-Jun-Chao"
Original title
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined to bottom left corner and top right corner as shown in the figure.
Rectangle Area
Assume that "total" is never beyond the maximum possible value of int.
The main effect of the topic
Calculates the total area covered by two rectangles.
ideas for solving problems
The calculation method is rectangle 1 area + Rectangle 2 Area-two rectangle common covering area.
Code Implementation
Algorithm implementation class
Import java.util.*;
public class Solution {public
int computearea (int A, int B, int C, int D, int E, int F, int G, int H) {
long area = (long) (c-a) * (d-b) + (Long) (G-E) * (h-f);
Math.min (C, G) indicates that the smallest edge
//Math.max (A, E) on the right side indicates that the largest edge of the left side
//below will result in an array-mediated expansion of the data range
long width = Math.max ( Long) math.min (C, G)-(long) Math.max (A, E), 0);
Math.min (d, h) represents the smallest side of the top edge
//Math.max (B, F) representing the largest edge of the bottom edge
long height = Math.max (long) math.min (d, h)-(long) Math . Max (B, F), 0);
return (int) (area-width * height);
}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, released in a new window to view the full picture.
Special Notes Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/48084065"