LeetCode 223 Rectangle Area (rectangular Area)

Source: Internet
Author: User

LeetCode 223 Rectangle Area (rectangular Area)
Translation

Find the total area of the two intersecting rectangles in the two-dimensional plane. Each rectangle defines the coordinates in the lower left corner and the upper right corner. (For example, a rectangle) assumes that the total floor space will never exceed the maximum value of an int.
Original

Analysis

I tried this question the day before yesterday and wrote a bunch of judgments. After all, it was still fruitless ......

Post several other people's solutions ......

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H){    int64_t xmin1 = min( A, C );    int64_t xmax1 = max( A, C );    int64_t ymin1 = min( B, D );    int64_t ymax1 = max( B, D );    int64_t xmin2 = min( E, G );    int64_t xmax2 = max( E, G );    int64_t ymin2 = min( F, H );    int64_t ymax2 = max( F, H );    int64_t xa = min( xmax1, xmax2 ) - max( xmin1, xmin2 );    int64_t ya = min( ymax1, ymax2 ) - max( ymin1, ymin2 );    int64_t z = 0, ca = max( xa, z ) * max( ya, z );    int64_t a1 = (xmax1 - xmin1) * (ymax1 - ymin1);    int64_t a2 = (xmax2 - xmin2) * (ymax2 - ymin2);    return a1 + a2 - ca;}
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {    int overlap = (min(C,G)-max(A,E))*(min(D,H)-max(B,F));    if ( min(C,G)<=max(A,E) || min(D,H)<=max(B,F) )        overlap = 0;    return (C-A)*(D-B)+(G-E)*(H-F)-overlap;}
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {    int common = (C <= E || A >= G || B >= H || D <= F) ? 0 : (min(C, G) - max(A, E)) * (min(D, H) - max(B, F));    return (C - A) * (D - B) + (G - E) * (H - F) - common;}

Related Article

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.