"223-rectangle Area (rectangular region)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
code Download "Https://github.com/Wang-Jun-Chao"
Original Question
Find the total area covered by and rectilinear rectangles in a 2D plane.
Each rectangle was defined by its bottom left corner and top right corner as shown in the figure.
Rectangle Area
Assume the total area is never beyond the maximum possible value of int.
Main Topic
Calculates the area covered by a total of two rectangles.
Thinking of solving problems
The method is calculated as the area of the rectangle 1 + the area of the rectangle 2-two rectangles with a common covering area.
Code Implementation
Algorithm implementation class
Import java.util.*; Public classSolution { Public int Computearea(intAintBintCintDintEintFintGintH) {LongArea = (Long) (c-a) * (d-b) + (Long) (G-E) * (H-F);//Math.min (C, G) represents the smallest edge of the right side //Math.max (A, E) indicates the largest edge of the left side //The subtraction below will result in an array of more than a medium, to enlarge the data range Longwidth = Math.max ((Long) Math.min (C, G)-(Long) Math.max (A, E),0);//Math.min (D, H) indicates the smallest edge of the top edge //Math.max (B, F) indicates the largest edge of the bottom edge LongHeight = 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, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/48084065"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java implementation" "223-rectangle area (rectangular region)"