"Leetcode-Interview algorithm classic-java implementation" "223-rectangle area (rectangular region)" __ Rectangular

Source: Internet
Author: User
"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"

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.