Leetcode 223 Rectangle Area

Source: Internet
Author: User

1. Description of the problem

Find the area of two rectangles.
  

2. Methods and Ideas

The intersection area of two rectangles can be calculated first, and then the area is rectangular with two rectangular area and minus intersection area.
Note: Although the area does not exceed the maximum value of int, the middle edge length operation may be more than, pay attention to the processing details, otherwise easily overflow.
  

Class Solution {public:intComputearea (intAintBintCintDintEintFintGintH) {intUnionarea =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; }};intMain () {solution Sol;printf("%d\ n", Sol.computearea (-3,0,3,4,0,-1,9,2));inta[8] = {-1500000001,0,-1500000000,1,1500000000,0,1500000001,1};//for(inti =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;}

Leetcode 223 Rectangle Area

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.