[LeetCode-interview algorithm classic-Java implementation] [223-Rectangle Area (rectangular Area)], leetcode -- java

Source: Internet
Author: User

[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.