[LeetCode-interview algorithm classic-Java implementation] [223-Rectangle Area (rectangular Area)], leetcode -- java
[223-Rectangle Area (rectangular Area )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Download the Code [https://github.com/wang-jun-chao]Original question
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.
Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.
Theme
Calculates the total area covered by the two rectangles.
Solutions
The calculation method is the area of rectangle 1 + the area of rectangle 2-the area shared by the two rectangles.
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 the smallest edge of the right side. // Math. max (A, E) indicates the largest edge on the left. // The subtraction below the edge will produce the more interface the array is, and the data range should be extended long width = 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 maximum side 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 Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/48084065]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.