Leetcode 223 Rectangle Area, leetcoderectangle

Source: Internet
Author: User

Leetcode 223 Rectangle Area, leetcoderectangle
1. Problem Description

Calculate the area of the two rectangles.
  

2. methods and ideas

You can first obtain the intersection area of the two rectangles, and then use the sum of the two rectangles minus the intersection area to represent the rectangle and area.
Note: although the area does not exceed the maximum value of int, the length of the middle side may exceed. Pay attention to the handling details; otherwise, overflow may occur.
  

class Solution {public:    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {        int unionArea =0;        long wlimit,hlimit;        wlimit = (long)min(C,G)-(long)max(A,E);        hlimit = min(D,H)-max(B,F);         if(wlimit >=0 && hlimit >= 0)        {            unionArea = (min(C,G)-max(A,E))* (min(D,H)-max(B,F));        }        else        {            unionArea = 0;        }        //printf("test:%lu\n",wlimit);          return (C - A)*(D - B) + (G - E)*(H - F) - unionArea;    }};int main(){    Solution sol;    printf("%d\n",sol.computeArea(-3,0,3,4,0,-1,9,2));    int a[8] = {-1500000001,0,-1500000000,1,1500000000,0,1500000001,1};    //for(int i = 0; i < 8; i ++) scanf("%d",&a[i]);    printf("%d\n",sol.computeArea(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]));    return 0;}

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.